将值添加到javascript变量 - 对象

时间:2014-02-18 15:21:57

标签: javascript jquery

我想在这里添加值:

var selected_value = {
    addtypeid: addtypeid,
    isnew: isnew,
    geographicareaid: geographicareaid,
    catid: catid,
    catid2: catid2,
    manufacturerid: manufacturerid,
    modelid: modelid,
    yearofmanufacturing_from: yearofmanufacturing_from,
    yearofmanufacturing_to: yearofmanufacturing_to,
};

使用此代码:

var categories = [];

$("input[name='categoriesfilters[]']:checked").each(function () {
    var id = $(this).attr("id");
    var value = $(this).val();
    categories[categories.length] = {
        id: value
    };
});

$.each(categories, function (id, value) {
    //alert('id : ' + id + 'value:' + value);
    selected_value.id = value;
});

但是,只有最后选中的复选框按以下格式添加:

id[id]  166

虽然我希望有格式的所有复选框:

166:166,

任何人都可以帮我这个吗?

问候,约翰

1 个答案:

答案 0 :(得分:2)

试试这个:

$("input[name='categoriesfilters[]']:checked").each(function () {
    var id = $(this).attr("id");
    var value = $(this).val();

    categories.push({
       id: id,
       value: value
    });
 });


$.each(categories, function (id, item) {
   //alert('id : ' + id + 'value:' + value);
   selected_value[item.id] = item.value;
});