想必有很多做前端同学都遇到了这么一个问题。

那就是checkbox。那就是我们通过jquery设置选中的时候,发现checked属性已经设置上去了

但是选中的样式却没有。

我们做一个简单的测试:看下面三张图片

通过上面三张图我们可以看到我们有两个checkbox默认是第一个选中。

我们通过点击将第二个设置为选中状态。

然后我们通过jquery的attr属性修改选中状态。我们神奇的发现,选中的状态已经变了,但是样式却没有变过来。

 $("#reply1").attr("checked", true);

有些同学可能要说了,上面的代码有问题。应该是 $("#reply1").attr("checked", “checked”);

然而我们发现这并没有什么卵用。。。。于是乎我们彻底凌乱了

这里我告诉大家不是我们的错,是jquery的问题。

使用jQuery设置checkbox为选择状态时,我们通常会使用

1
2
3
  1. $<span style="color:rgb(0,153,0)">(</span><span style="color:rgb(0,0,102)"><strong>this</strong></span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">attr</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">"checked"</span><span style="color:rgb(51,153,51)">,</span> <span style="color:rgb(0,51,102)"><strong>true</strong></span><span style="color:rgb(0,153,0)">)</span><span style="color:rgb(51,153,51)">;</span>
  2. <span style="color:rgb(0,102,0)"><em>//或者</em></span>
  3. $<span style="color:rgb(0,153,0)">(</span><span style="color:rgb(0,0,102)"><strong>this</strong></span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">attr</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">"checked"</span><span style="color:rgb(51,153,51)">,</span> checked<span style="color:rgb(0,153,0)">)</span><span style="color:rgb(51,153,51)">;</span>

但是当你的jquery版本大于1.6的时候,这种只能生效一次,后面你只会看到checkbox的checked属性会增加checked但是现实的状态是没有打钩的。这种情况下你就不能使用$(this).attr(“checked”, checked)这种方法了,需要使用下面的代码:

1
  1. $<span style="color:rgb(0,153,0)">(</span><span style="color:rgb(0,0,102)"><strong>this</strong></span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">prop</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">"checked"</span><span style="color:rgb(51,153,51)">,</span> <span style="color:rgb(0,51,102)"><strong>true</strong></span><span style="color:rgb(0,153,0)">)</span><span style="color:rgb(51,153,51)">;</span>

prop()是jquery1.6以上版本的新方法,为什么会有这个方法呢?

prop和attr的区别

  • prop(propertyName) —— 获取匹配集合中第一个元素的Property的值

它还有另外几种重构函数,如下:

  • prop(propertyName, value)
  • prop(map)
  • prop(propertyName, function(index, oldPropertyValue))

上面的方法是给匹配元素集合设定一个或多个属性。

关于jQuery1.6和1.6.1中Attributes模块变化的描述,以及.attr()方法和.prop()方法的首选使用,如下所示:

Attributes模块的变化是移除了attributes和properties之间模棱两可的东西,但是在jQuery社区中引起了一些混乱,因为在1.6之前的所有版本中都使用一个方法(.attr())来处理attributes和properties。但是老的.attr()方法有一些bug,很难维护。jQuery1.6.1对Attributes模块进行了更新,并且修复了几个bug。

elem.checked true (Boolean) Will change with checkbox state
$(elem).prop(“checked”) true (Boolean) Will change with checkbox state
elem.getAttribute(“checked”) “checked” (String) Initial state of the checkbox; does not change
$(elem).attr(“checked”)(1.6) “checked” (String) Initial state of the checkbox; does not change
$(elem).attr(“checked”)(1.6.1+) “checked” (String) Will change with checkbox state
$(elem).attr(“checked”)(pre-1.6) true (Boolean) Changed with checkbox state

1
2
3
  1. <span style="color:rgb(0,0,102)"><strong>if</strong></span><span style="color:rgb(0,153,0)">(</span>elem.<span style="color:rgb(102,0,102)">checked</span><span style="color:rgb(0,153,0)">)</span>
  2. <span style="color:rgb(0,0,102)"><strong>if</strong></span><span style="color:rgb(0,153,0)">(</span>$<span style="color:rgb(0,153,0)">(</span>elem<span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">prop</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">"checked"</span><span style="color:rgb(0,153,0)">)</span><span style="color:rgb(0,153,0)">)</span>
  3. <span style="color:rgb(0,0,102)"><strong>if</strong></span><span style="color:rgb(0,153,0)">(</span>$<span style="color:rgb(0,153,0)">(</span>elem<span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(0,0,102)"><strong>is</strong></span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">":checked"</span><span style="color:rgb(0,153,0)">)</span><span style="color:rgb(0,153,0)">)</span>

以上三个都是返回Boolean值。
为了让jQuery1.6中的.attr()方法的变化被理解的清楚些,下面是一些使用.attr()的例子,虽然在jQuery之前的版本中能正常工作,但是现在必须使用.prop()方法代替:

attr() prop()
$(window).attr… $(window).prop…
$(document).attr… $(document).prop…
$(“:checkbox”).attr(“checked”,true) $(“:checkbox”).prop(“checked”,true)
$(“:option”).attr(“checked”,true) $(“:option”).prop(“checked”,true)

首先,window或document中使用.attr()方法在jQuery1.6中不能正常运行,因为window和document中不能有attributes。它们包含properties(比如:location或readyState),必须使用.prop()方法操作或简单地使用javascript原生的方法。在jQuery1.6.1中,window和document中使用.attr()将被自动转成使用.prop,而不是抛出一个错误。

其次,checked,selected和前面提到的其它boolean attributes,因为这些attributes和其相应的properties之间的特殊关系而被特殊对待。基本上,一个attribute就是以下html中你看到的:

1
  1. <input type="checkbox" checked="checked">

boolean attributes,比如:checked,仅被设置成默认值或初始值。在一个checkbox的元素中,checked attributes在页面加载的时候就被设置,而不管checkbox元素是否被选中。

properties就是浏览器用来记录当前值的东西。正常情况下,properties反映它们相应的attributes(如果存在的话)。但这并不是boolean attriubutes的情况。当用户点击一个checkbox元素或选中一个select元素的一个option时,boolean properties保持最新。但相应的boolean attributes是不一样的,正如上面所述,它们仅被浏览器用来保存初始值。

1
2
3
4
  1. $<span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">":checkbox"</span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">get</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(204,0,0)">0</span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">checked</span> <span style="color:rgb(51,153,51)">=</span> <span style="color:rgb(0,51,102)"><strong>true</strong></span><span style="color:rgb(51,153,51)">;</span>
  2. <span style="color:rgb(0,102,0)"><em>// Is the same as $(":checkbox:first").prop("checked", true);  </em></span>
  3. 在jQuery1.6中,如果使用下面的方法设置checked:
  4. $<span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">":checkbox"</span><span style="color:rgb(0,153,0)">)</span>.<span style="color:rgb(102,0,102)">attr</span><span style="color:rgb(0,153,0)">(</span><span style="color:rgb(51,102,204)">"checked"</span><span style="color:rgb(51,153,51)">,</span> <span style="color:rgb(0,51,102)"><strong>true</strong></span><span style="color:rgb(0,153,0)">)</span><span style="color:rgb(51,153,51)">;</span>

以上代码将不会检查checkbox元素,因为它是需要被设置的property,但是你所有的设置都是初始值。

然而,曾经jQuery1.6被释放出来的时候,jQuery团队明白当浏览器仅关心页面加载时,设置一些值不是特别的有用。所以,为了保持向后兼容性和.attr()方法的有用性,我们可以继续在jQuery1.6.1中使用.attr()方法取得和设置这些boolean attributes。

最普通的attributes是checked,selected,disabled和readOnly,但下面是jQuery1.6.1支持的使用.attr()动态地取得和设置boolean attributes/properties的完整列表:autofocus, autoplay, async, checked, controls, defer, disabled, hidden, loop, multiple, open, readonly, required, scoped, selected。

还是建议使用.prop()方法来设置这些boolean attributes/properties,即使这些用例没有转换成使用.prop()方法,但是你的代码仍然可以在jQuery1.6.1中正常运行。

下面是一些attributes和properties的列表,正常情况下,应该使用其对应的方法(见下面的列表)来取得和设置它们。下面的是首用法,但是.attr()方法可以运行在所有的attributes情况下。

注意:一些DOM元素的properties也被列在下面,但是仅运行在新的.prop()方法中

.attr()和.prop()都不应该被用来取值/设值。使用.val()方法代替(即使使用.attr(“value”,”somevalue”) 可以继续运行,就像1.6之前做的那样)

首选用法的概述

prop()方法应该被用来处理boolean attributes/properties以及在html(比如:window.location)中不存在的properties。其他所有的attributes(在html中你看到的那些)可以而且应该继续使用.attr()方法来进行操作。

input---checked小问题的更多相关文章

  1. input:checked + label用法

    input:checked ~ label   :相邻同胞选择器,选择被选中的input标签后 所有的label标签[input  和 label标签有共同的父元素]: input:checked + ...

  2. jquery检测input checked 控件是否被选中的方法

    jquery检测input checked 控件是否被选中 js部分 复制代码代码如下: function tongyianniu(){ var gouxuan=$('input[type=check ...

  3. 重置input checked

    <!-- 作者:duke 时间:2018-10-24 描述: 重置input 样式--> <!DOCTYPE HTML><html> <head> &l ...

  4. 支付宝小程序input的小坑

    //axml<input class="internet_input" value="{{payNo}}" onInput="keyNum&qu ...

  5. input使用小技巧

    ①:如何修改placeholder样式? input::-webkit-input-placeholder { color: #ccc; font-size: 15px; } 注:其它浏览器适配方案 ...

  6. input的小技巧 清除自动记录

    input 消除自动记忆功能 在html里就可以直接清除了<input type="text" autocomplete="off"> input ...

  7. input 被checked时和label配合的妙用

    input 和label配合的妙用 1:作为文字隐藏与否的开关: 如下代码:对div里面所包含的文字较多,一开始只展示小部分,当用户点击按钮时,进行全部内容的展示(按钮是以向下隐藏箭头的图片) htm ...

  8. 仅IE6/7中添加checked为true的input到DOM中为false

    HTML INPUT元素有个checked属性,多数情况type为radio和checkbox. 当创建一个input,checked属性赋值为true,添加到DOM文档中,当再次取checked属性 ...

  9. input 选择框改变背景小技巧

    最近在项目中遇到一个问题,想要改变input选择框的背景,然而,令我没有想到的是,竟然无法直接改变背景的颜色 通常情况下:我们都可以通过改变元素的 background-color 的值来改变元素的背 ...

  10. html input复选框的checked属性

    input --checked: 只要复选框有checked属性,不管属性值为空或者为true or false或任意值,复选框都会被选中.切忌:checked属性值不要带引号 <input t ...

随机推荐

  1. .net之微信企业号开发(一) 所使用的环境与工具以及准备工作

    前言 一直以来,从事的是.net winform的编程,虽然对移动互联这块很感兴趣,但是由于现有的工作和移动互联之间隔的太远,也就没有时间和精力好好的去研究和实现.今年年初辞职了,刚好朋友那里希望建立 ...

  2. mentohust 你让我如何说你是好呢?

    最近换了ubuntu系统结果热了不少的麻烦,  借此机会唠叨一下, 首先是你这个ubuntu16.4 你这个bug 太让人郁闷了吧,或许主要是应该怪我菜,装个看家的软件eclipse.还热除了一堆的麻 ...

  3. video标签无法使用的问题

    原因:IIS的MIME中未注册MP4.ogg.webm相关类型,导致IIS无法识别 解决方法:在IIS中注册MP4.ogg.webm类型,以下以MP4为例,ogg和webm以此类推: windows ...

  4. C++内存对齐的理解

    程序编译器对结构的存储的特殊处理确实提高CPU存储变量的速度,但是有时候也带来了一些麻烦,我们也屏蔽掉变量默认的对齐方式,自己可以设定变量的对齐方式. 编译器中提供了#pragma pack(n)来设 ...

  5. JavaScript 基础第七天(DOM的开始)

    一.引言 JavaScript的内容分为三个部分,这三个部分分别是ECMAScript.DOM.BOM三个部分组成.所谓ECMAScript就是JavaScript和核心基础语法,DOM是文档对象模型 ...

  6. linux shell 字符串操作

    转:http://justcoding.iteye.com/blog/1963463 在做shell批处理程序时候,经常会涉及到字符串相关操作.有很多命令语句,如:awk,sed都可以做字符串各种操作 ...

  7. 【转】java.lang.OutOfMemoryError: Java heap space的解决

    原文地址:http://blog.sina.com.cn/s/blog_4b12778b0100v0bb.html Myeclipse下java.lang.OutOfMemoryError: Java ...

  8. Linux CentOS下如何确认MySQL服务已经启动

    Linux CentOS一般做为服务器使用,因此,MySQL服务应该随开机自动启动的.正常情况下,查看开机自动启动的服务使用chkconfig命令,如下: #chkconfig --list 实际使用 ...

  9. NetMq学习--发布订阅(一)

    基于NeqMq 4.0.0-rc5版本发布端: using (var publisher = new PublisherSocket()) { publisher.Bind("tcp://* ...

  10. Java多线程15:Queue、BlockingQueue以及利用BlockingQueue实现生产者/消费者模型

    Queue是什么 队列,是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的.无论使用哪种排序方式,队列的头都是调用remove()或poll()移 ...