简体   繁体   English

JavaScript 推送到阵列 object 不起作用

[英]JavaScript push to array object is not working

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...?我正在尝试先设置并再次检索以进行检查,但什么也没有发生……我不明白我错在哪里……?

I guess you missed to get the stringify result into stringData variable 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 错误。

JSON or JSON.stringify is not provided by jQuery you have to include json2 library on the page if the browser natively do not support it. JSONJSON.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'

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/

In your example, you never set stringData .在您的示例中,您从未设置stringData

get/set the "value" property directly on the input, not getAttribute and setAttribute methods.直接在输入上获取/设置“值”属性,而不是 getAttribute 和 setAttribute 方法。

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 调试器)的环境中运行,则在错误控制台中很容易看到此类错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM