我有一个带有很多复选框的表单。
复选框的名称和数量是动态创建的。
当我单击这些复选框中的任何一个时,如何获取 jquery onClick 事件?
<form id="myform">
<input type="checkbox" name="chk_123">
<input type="checkbox" name="chk_456">
<input type="checkbox" name="chk_23">
<input type="checkbox" name="chk_3">
<input type="checkbox" name="chk_443">
<input type="checkbox" name="chk_6764">
</form>
我希望使用表单的 ID 来选择它们,在本例中是属于表单 #myform 的所有复选框。
我问这个问题是因为我不想只向每个复选框添加类名并按类名进行选择。
我的目标:一个警告框“您刚刚单击了名称为 chk_{whatever the number is} 的复选框”
最佳答案
使用属性选择器:
$('#myform input[type="checkbox"]').change(function() {
alert("You just clicked checkbox with the name " + this.name)
});
关于单击特定表单的任何复选框时的 jquery 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22908925/