2013-04-10 107 views
0
$('#Container').append('<input type="checkbox" id = '+ data[i].name + '/> ' + data[i].name + '<br />'); 

上面的代码在框A(div)中显示了一些复选框。如果我在框A中选择一个复选框,则将该值发送到数据库,并获取将显示在框B中的值。在选择复选框时取决于负载的复选框

例如,如果我在框A中选择2个复选框,则会将这些值到数据库中,它会获取相应的值,并在盒子B.显示它们

$('#checkbox').change(function() { 

我想知道这是什么#checkbox指。它是复选框或其他东西的ID吗?

有什么办法可以实现这个吗?

+0

'#checkbox'参阅的的元件具有ID的复选框 – 2013-04-10 10:58:15

回答

1

是的,它是一个ID ..

$('#checkbox') <-- refers to an element having an id as "checkbox" it might be div or checkbox or any element whose id is equal to checkbox. 
$('.checkbox') <--refers to an element having class as "checkbox" 
$('input:checkbox') <--- refers to all input with type checkbox 

经过文档,详细了解selectors

更新

例如

$('#Container').append('<input type="checkbox" class="ckbox" id = '+ data[i].name + '/> ' + data[i].name + '<br />'); 
$('#Container').append('<input type="checkbox" class="ckbox" id = '+ data[i].name + '/> ' + data[i].name + '<br />'); 

调用类选择

$('#Container').on('change','.ckbox',function(){ 
    var selectedValue = $("input.ckbox:checked").map(function(n){ 
     return this.value; 
    }); 
    alert(seletedValue.join(',')); 
}); 

on()因为检查是动态添加,从而不得不委派发生变化事件的事件。

+0

我有数据[I]。名称为ID,这意味着I L b得到许多ids.how找零功能吗? – sahana 2013-04-10 11:28:01

+0

@sahana首先确保你所有的ID都是唯一的..如果是...你可以用一个类命名所有的复选框(相同),并为更改事件调用类选择器。更新我的答案..检查它出 – bipen 2013-04-10 11:43:02

+0

是的,我有你的观点。我只想收集选中的值(也是未选中的值)作为列表并将列表发送到此更改函数。我创建复选框的地方是一个单独的方法。这种变化应该是分开的。 – sahana 2013-04-10 12:09:33