jQuery 代码问题

             //对于布尔类型的属性,不要用attr方法,应该用prop方法。 prop用法跟attr方法一样。
             $("#btn1").click(function () {
                 // $("#ck").attr("checked","true");
                 $("#ck").prop("checked",true);//true不能加引号
             });
             $("#btn2").click(function () {
                 $("#ck").prop("checked",false);
             });

请问这里的false和true为什么不需要加引号,平常使用需要加引号的原因是什么?

阅读 2.7k
5 个回答

checked本来就是一个bool值,你为啥要加引号,加引号不变成string了吗?

prop("checked", "false") 也会被当成勾选对待,这肯定不是你想要的效果吧?

加双引号不就是字符串了吗?1楼贴得那张图里已经写得很清楚了,是需要boolean类型的值。

真心建议先把DOM和JS的基础再去撸一遍...

https://developer.mozilla.org...
仅适用于类型为"checkbox" 或 "radio"元素的属性

checked boolean: Returns / Sets the current state of the element when type is checkbox or radio.
defaultChecked boolean: Returns / Sets the default state of a radio button or checkbox as originally specified in HTML that created this object.
indeterminate boolean: indicates that the checkbox is neither on nor off. Changes the appearance to resemble a third state. Does not affect the value of the checked attribute, and clicking the checkbox will set the value to false.

建议买本书好好学学DOM和js基础

加引号不成字符串了,要的是布尔类型

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题