如果选中或取消选中复选框,如何捕获(How to capture if checkbox is checked or unchecked)
我有一个表,每列都有一个复选框,它有一个类,但没有id或名称。 选中或取消选中该复选框时,我使用alert()显示复选框状态:
$(document).on('change', '[class*="box"]', function () { var tableRow = $(this).closest('tr'); alert('check state ' + $("[class*='box']").is(':checked') ? 1 : 0); });无论是选中还是取消选中复选框,我的alert()始终返回1 。 也许我的编码方法可能是错的。 任何人都可以帮助获得正确的复选框状态吗? 谢谢
I have a table and in each column it has a checkbox which has a class but no id or name. When the checkbox is checked or unchecked I am showing the checkbox state using an alert():
$(document).on('change', '[class*="box"]', function () { var tableRow = $(this).closest('tr'); alert('check state ' + $("[class*='box']").is(':checked') ? 1 : 0); });My alert() always returns 1 whether the checkbox is checked or unchecked. Maybe my coding approach could be wrong. Can anyone help to get the right state of checkbox? Thanks
最满意答案
使用this :
$(document).on('change', '[class*="box"]', function () { var tableRow = $(this).closest('tr'); alert('check state ' + ((this.checked) ? 1 : 0)); });三元条件也需要额外的括号才能更好地发挥作用。
Use this:
$(document).on('change', '[class*="box"]', function () { var tableRow = $(this).closest('tr'); alert('check state ' + ((this.checked) ? 1 : 0)); });Also ternary conditions need extra brackets for better functioning.
更多推荐
发布评论