我的代码的目的是将所选 HTML 表格行中的数据添加到 JavaScript 数组,如果该行未被选中则将其删除。
https://jsfiddle.net/nicktry/h1636ram/
我现在有两个问题:
- 第一次点击时未应用 CSS 样式
我无法让 jQuery 选择器正确地向数组添加/删除表数据。
$('table tbody tr').click(function(event) { if (event.target.type !== 'checkbox') { $(':checkbox', this).trigger('click'); } $("input[type='checkbox']").change(function(e) { if($(this).is(":checked")){ $(this).closest('tr').addClass("selected_row"); addJob(document.getElementById('jobs'), $(this).closest('tr td:nth-child(2)').text()); }else{ $(this).closest('tr').removeClass("selected_row"); removeJob(document.getElementById('jobs'), $(this).closest('tr td:nth-child(2)').text()); } }); });
不胜感激 stackoverflow 社区的智慧!
最佳答案
你不应该有两个事件在彼此内部。
移动这个
$("input[type='checkbox']").not('#check_all').change(function (e) {
});
在
之外 $('table tbody tr').click(function(event) {
});
改变这个
$(this).closest('tr td:nth-child(2)').text()
对此
$(this).parent().next().text()
关于javascript - HTML 表格行中第二列的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30007765/