jQuery接收复选框状态 [英] jQuery receiving checkbox status

查看:128
本文介绍了jQuery接收复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式更改复选框状态: $(this).attr(checked,'checked');



这之后,我想要接收复选框状态,但我得到了:

  $(this).attr ('checked'):checked
$(this).is(':checked'):false


b $ b

这是怎么做的?



PS也许我没有通过jQuery正确改变checkbox的状态?

解决方案

正确的方式是 $ ).prop('checked')返回布尔属性而不是属性(它是一个字符串)。



使用 .prop()您也可以设置检查状态: $(this)正如你可以在 > http://jsfiddle.net/ThiefMaster/QR2fL/ .attr('checked')返回属性的初始值 - 它不勾选/取消勾选核取方块时会变更。


I'm changing checkbox status this way: $(this).attr("checked", 'checked');

After this I want to receive checkbox status, but I got this:

$(this).attr('checked'): "checked"
$(this).is(':checked'): false

How this can be?

P.S. Maybe I'm not correctly changing checkbox status via jQuery?

解决方案

The proper way is $(this).prop('checked') to return the boolean property instead of the attribute (which is a a string).

Using .prop() you can also set the checked state: $(this).prop('checked', true_or_false);

As you can see on http://jsfiddle.net/ThiefMaster/QR2fL/, .attr('checked') returns the initial value of the attribute - it does not changed when checking/unchecking the checkbox.

这篇关于jQuery接收复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆