2011-11-18 131 views
1

我有一个自定义的asp.net控件,其中包含一些复选框。我知道怎么弄的复选框,这是点击通过点击获取所有选中的复选框

$('#customer-category-control input:checkbox').click(function(event) 
{ 
    var el = $(this).attr('name'); 
}; 

建议我请,如何获得唯一全部由点击选中的复选框,并从他们的名字做一个JSON对象。

+0

嗯...我看到各种不同的元素混合起来的事件。你为什么要听'复选框'上的点击事件?首先附加一个'change'事件,当你要捕捉'submit'事件时,你可以通过'$('#customer-category-control input:checkbox:checked')'获得一个选中的复选框列表。 – Shef

回答

4
var names=[]; 
$('#customer-category-control input[type="checkbox"]:checked').each(function (i, el){ 
    names.push(el.name); 
    }); 
console.log(names); // => ['foo','bar'...] 
2

试试这个:

var obj = []; 
$('#customer-category-control input[type=checkbox]:checked').each(function(index, value) { 
    obj.push($(this).attr("name")); 
}); 
+0

我必须通过点击来完成。 – Alexandre

+0

你想在哪里点击?你没有提到这一点 –

0
$(document).ready(function() 
     { 
      $('#customer-category-control input:checkbox').click(function(event) 
       { 
       var obj = []; 
       $('#customer-category-control input[type=checkbox]:checked').each(function(index, value) 
        { obj.push($(this).attr("name"));  }); 
alert(obj); 

      }); 
     }); 
相关问题