$(this).attr('checked')得到的值为undefined

当导入<script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.4.0/jquery.js"></script>时

$(this).attr('checked')可以正常获取true|false,但是,

当导入<script type="text/javascript" src="https://cdn.bootcss.com/jquery/2.0.0/jquery.js"></script>时,

$(this).attr('checked')得到的值为undefined。

查询得知:来jquery 1.6以前用$(this).attr('checked')得到的是true/false,但到了1.6以后,$(this).attr('checked')就有点问题了。

  解决方案如下:

    1. 使用is()

        例句: $(this).is(":checked");             // 注意是':checked',有冒号的!

    2. 使用prop()方法,JQ1.6之后,可以通过attr方法去获得属性,通过prop方法去获得特性,属性指的是“name,id”等等,特性指的是“selectedIndex, tagName, nodeName”等等。 

        例句: $(this).prop('checked');

参考自:https://www.cnblogs.com/andy-zheng/p/6029259.html

猜你喜欢

转载自blog.csdn.net/qq_34361514/article/details/86062947