我有一个表,每行的第一列中只有复选框。我想找到使用 Jquery 单击了哪个复选框(更改事件)。我尝试了这个,但没有显示任何内容:
$('#table tbody tr td input[type=checkbox]').change(function () {
if (this.checked) {
alert("true");//for testing purposes
}
alert("false");
});
如何获得被点击的元素?
<table id="table" class="table table-hover table-striped table-bordered">
<thead>
<tr>
<th class="col-lg-1">Sıra No.</th>
<th class="col-lg-3">İsim/No</th>
<th class="col-lg-6">Koordinat Bilgisi</th>
<th class="col-lg-2">Mahalle/Kapı</th>
</tr>
</thead>
<tbody id="tableBody">
</tbody>
</table>
编辑:我的表体一开始是空的。单击按钮后我正在填充表格主体。每行第一列都有一个使用纯 javascipt 创建的输入元素。
var indexCheckbox = document.createElement('input');
indexCheckbox.type = "checkbox";
indexCheckbox.name = "indexCheckbox";
indexCheckbox.value = i;
indexCheckbox.id = "indexCheckbox_" + i;
最佳答案
事件委托(delegate)方法仅将事件处理程序附加到一个元素(复选框),并且事件只需要向上冒泡一级(从单击的复选框到#tableBody):
$('#tableBody').on("change", "input[type=checkbox]", function () {
if (this.checked) {
alert("true");//for testing purposes
}else{
alert("false");
}
});
关于javascript - 如何使用 JQuery 查找表中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37364812/