在jQuery中获取数组中复选框列表的值 [英] get value of checked checkbox list in array in jQuery

查看:220
本文介绍了在jQuery中获取数组中复选框列表的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



首先,我要读取所有复选框(选中)的值并存储在全局数组。



稍后,当用户单击任何复选框(未选中/选中)时,我想更新数组只有复选框的值。



这一切我想在jQuery中做。



感谢

  var arrCheckboxes; 
var checkboxSelector =input [type ='checkbox'];
$(body)。delegate(checkboxSelector,click,function(){
arrCheckboxes = $(checkboxSelector).map(function(){
return this.checked;
})。get();
});

(也许您应该将$(body)更改为更精确的容器)



如果你想要一个对象名为(...或id或者可能是元素)的数组...你可以这样做:

  var arrCheckboxes; 
var checkboxSelector =input [type ='checkbox'];
$(body)。delegate(checkboxSelector,change,function(){
arrCheckboxes = $(checkboxSelector).map(function(){
return {name:this。 name,val:this.checked};
})。get();
});


I have list of checkboxes that has all checkbox pre-checked when page load.

Firstly, I want to read all checkboxes (checked) value and store in global array.

Later, whenever any checkbox is clicked by user (un-checked / checked), I want to update the array with values of only checked checkboxes.

All this i want to do in jQuery.

thanks

解决方案

Did you mean something like this?

var arrCheckboxes;
var checkboxSelector = "input[type='checkbox']";
$("body").delegate(checkboxSelector , "click", function(){
    arrCheckboxes = $(checkboxSelector).map(function() {
        return this.checked;
    }).get();
});

(Maybe you should change the $("body") to a more precise container)

If you want an array with objects with name (...or id or maybe the element)... you can do something like this:

var arrCheckboxes;
var checkboxSelector = "input[type='checkbox']";
$("body").delegate(checkboxSelector , "change", function(){
    arrCheckboxes = $(checkboxSelector).map(function() {
        return { name: this.name, val: this.checked };
    }).get();
});

这篇关于在jQuery中获取数组中复选框列表的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆