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还是其他?

有什么方法可以实现吗?

4

1 回答 1

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

浏览文档以了解有关选择器的更多信息

更新

例子

$('#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()因为检查是动态添加的,所以必须委托事件才能发生更改事件。

于 2013-04-10T10:59:05.300 回答