如果选中或取消选中复选框,如何捕获(How to capture if checkbox is checked or unchecked)

编程入门 行业动态 更新时间:2024-10-24 13:19:07
如果选中或取消选中复选框,如何捕获(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.

更多推荐

本文发布于:2023-08-05 07:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1430240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:复选框   capture   checkbox   checked   unchecked

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!