JavaScript 推送到数组对象不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7097733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 22:58:01  来源:igfitidea点击:

JavaScript push to array object is not working

javascriptjson

提问by user882196

I have an html like below

我有一个像下面这样的 html

HTML:

HTML:

 <INPUT TYPE=CHECKBOX NAME="clcik" onClick="add('1234','blah')" />
 <input type="hidden" id="project" value="" />

JS:

JS:

    function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };
    jsonArray.push(jsonObj);
    var field = document.getElementById('project');
    field.setAttribute('value', JSON.stringify(jsonArray));
    alert(field.getAttribute('value'));
    }

I am trying to set first and retrieve again to check but nothing is happening..I can't understand where I am wrong...?

我试图先设置并再次检索以检查但没有发生任何事情..我不明白我错在哪里......?

回答by ShankarSangoli

I guess you missed to get the stringify result into stringDatavariable because of which you are getting a js error before it reaches the line where you are trying to alert the value.

我猜你错过了将 stringify 结果放入stringData变量中,因为在它到达你试图警告值的行之前你会收到一个 js 错误。

JSONor JSON.stringifyis not provided by jQueryyou have to include json2library on the page if the browser natively do not support it.

JSON或者JSON.stringify没有提供由jQuery你必须包括json2在页面上库如果浏览器本身不支持它。

Try this

试试这个

function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };

    jsonArray.push(jsonObj);
    var stringData = JSON.stringify(jsonArray);

    var field = document.getElementById('project');

    field.setAttribute('value', stringData);
    alert(field.getAttribute('value'));
}

Code to remove element from array based on your request in the comment.

根据您在评论中的请求从数组中删除元素的代码。

var newArray = [], productIdToRemove = "1234";
$.each(jsonArray, function(){
   if(this.product_id != productIdToRemove){
      newArray.push(this);
   }
});

//Now newArray will not have '1234'

回答by Matt Ball

Change

改变

JSON.stringify(jsonArray)
// to
var stringData = JSON.stringify(jsonArray);


Other problems with the fiddle, fixed: http://jsfiddle.net/mattball/qWCwa/7/

小提琴的其他问题,已修复:http: //jsfiddle.net/mattball/qWCwa/7/

回答by FishBasketGordo

In your example, you never set stringData.

在您的示例中,您从未设置stringData.

回答by Stephen

get/set the "value" property directly on the input, not getAttribute and setAttribute methods.

直接在输入上获取/设置“value”属性,而不是 getAttribute 和 setAttribute 方法。

回答by jfriend00

After changing the code to assign stringData, it seems to work for me:

更改代码以分配 stringData 后,它似乎对我有用:

function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };
    jsonArray.push(jsonObj);
    var stringData = JSON.stringify(jsonArray)
    var field = document.getElementById('project');
    field.setAttribute('value', stringData);
    alert(field.getAttribute('value'));
    }

add(1, 2);

You can see it working here: http://jsfiddle.net/jfriend00/HNuak/.

你可以在这里看到它的工作:http: //jsfiddle.net/jfriend00/HNuak/

FYI, if you just run in an environment where you can see javascript execution errors (like the Chrome or Firefox debuggers), errors like this would be easy to see in the error console.

仅供参考,如果您只是在可以看到 javascript 执行错误的环境中运行(如 Chrome 或 Firefox 调试器),那么在错误控制台中很容易看到这样的错误。