jquery选中radio或checkbox的正确姿势

Intro

前几天突然遇到一个问题,没有任何征兆的。。,jquery 选中radio button单选框时,一直没有办法选中,后来查了许多资料,发现自己的姿势有问题。

Issue

我按下面的方式选中 radio 时,发现有时候会选不中。

  <label class="t-radio t-circle-radio t-green-radio"><input type="radio" value="1" onchange="$('#saleInfo').show()" checked="checked" name="isOnSale" />加入</label>
<label class="t-radio t-circle-radio t-green-radio"><input type="radio" value="0" onchange="$('#saleInfo').hide()" name="isOnSale" />不加入</label>

下面是我的 js 代码

    //前面已引用 jquery.js 后文不再赘述
...
$("input[type='radio'][name='isOnSale'][value='1']").attr("checked","checked");

Solution0

区分 attribute 和 property

attribute 和 property 是不同的

property 是 html 标签固有的属性,而 attribute 多是 html 自定义属性。

attribute是html文档上标签属性,而 property 则是对应 DOM 元素的自身属性。 从操作方法上来看,attribute可以通过 DOM规范的 getAttributesetAttribute进行获取修改,而property可以通过对象访问属性的方式 . 或者 [" "]来修改获取。 jquery 获取或设置 attribute 用的是 attr ,获取或设置 property 用的是 prop

Property

DOM 节点是一个对象,所以它像 JavaScript 的对象一样可以存储自定义的属性和方法。

Attribute

DOM节点可以通过以下标准方法访问 HTML 的 attribute

    elem.hasAttribute(name) - checks if the attribute exists
elem.getAttribute(name) - gets an attribute value
elem.setAttribute(name, value) - sets an attribute
elem.removeAttribute(name) - removes an attribute

checked 是 input 标签的属性(property)而不是 attribute ,参见 http://www.w3school.com.cn/tags/att_input_checked.asp

更多 input 相关的属性详见: http://www.w3school.com.cn/tags/tag_input.asp

Solution1

HACK:mock click

设置选中之后调用对象的click()方法,模拟点击

    //toogle
$("input:radio[name='isOnSale'][value='1']").click();

Solution2

设置 input 的 checked 属性

原生js

    //check
document.getElementsByName("isOnSale")[0].checked = true;
//uncheck
document.getElementsByName("isOnSale")[0].checked = false;

jquery

    //
$("input[type='radio'][name='isOnSale'][value='1']").prop("checked",true);

More

如果你有别的更好的解决方案,欢迎指出。

如果有什么问题,欢迎联系我 ben121011@126.com

jquery选中radio或checkbox的正确姿势的更多相关文章

  1. jquery的radio和checkbox的标签的操作集合

    jquery的radio和checkbox的标签的操作集合: $("input[name='radio_name'][checked]").val(); //选择被选中Radio的 ...

  2. JQuery触发radio或checkbox的change事件

    在JQuery中,当给radio或checkbox添加一个change事件时,如果它的值发生变化就会触发change事件;本文将详细介绍如何利用JQuery触发Checkbox的change事件需要了 ...

  3. jquery实用应用之jquery操作radio、checkbox、select

    本文收集一些jquery的实用技巧,非常实用的哦,其中对radio.checkbox.select选中与取值的方法. 获取一组radio被选中项的值var item = $('input[@name= ...

  4. jquery对radio和checkbox的操作

    jQuery获取Radio选择的Value值 代码  $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Valu ...

  5. 《jQuery判断radio、checkbox、select 是否选中和设置选中问题以及获取选中值》总结

    <form> <input type="radio" name="gender" id="man" value=" ...

  6. [转]jQuery操作radio、checkbox、select 集合.

    1.radio:单选框 html代码 <input type="radio" name="radio" id="radio1" val ...

  7. jQuery操作radio、checkbox、select 集合

    1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...

  8. jQuery操作radio、checkbox、select总结

    1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...

  9. JQuery 选中Radio

    <tr> <td> <input type="radio" name="rdb" value="启用" che ...

随机推荐

  1. SpringMVC源码情操陶冶-AbstractHandlerExceptionResolver

    springmvc支持服务端在处理业务逻辑过程中出现异常的时候可以配置相应的ModelAndView对象返回给客户端,本文介绍springmvc默认的几种HandlerExceptionResolve ...

  2. 极简的Android RecyclerView Adapter(使用DataBinding)

    阅读本篇文章需要读者对Android Databinding和RecyclerView有一定的了解. 简介 我们知道,DataBinding的核心理念是数据驱动.数据驱动驱动的目标就是View,使用D ...

  3. HTML随笔1

    1.编号列表: <ol type="A" start="1">    //type中有"A","1",&qu ...

  4. JAVA 中BIO,NIO,AIO的理解以及 同步 异步 阻塞 非阻塞

    在高性能的IO体系设计中,有几个名词概念常常会使我们感到迷惑不解.具体如下: 序号 问题 1 什么是同步? 2 什么是异步? 3 什么是阻塞? 4 什么是非阻塞? 5 什么是同步阻塞? 6 什么是同步 ...

  5. JavaWeb学习归档(一) - HTTP协议总结

    版权声明:本文为博主原创文章,未经博主允许不得转载. 本文转载自我的个人博客:http://www.codingme.net/post/java-web-01 HTTP协议简介 超文本传输协议(HTT ...

  6. R语言成功加载rJava方法

    加载rJava的同时,要下载JAVA的JRE并且配置环境变量JAVA_HOME,因为rJava的调用需要java运行环境. 1.下载JRE 64位版本的JRE官网下载:http://www.java. ...

  7. 独立ip的优势

    独立ip的建站优势   我想很多人都想知道,那我就在这里给大家简单介绍下独立ip的优势有那些.                    网站设计是需要很多专业知识的结合,整站不是那么容易就可以设计完成的 ...

  8. ABP+AdminLTE+Bootstrap Table权限管理系统第十一节--bootstrap table之用户管理列表

    这张开始bootstrap table,引入项目有两种方法,一种是直接去官网下载 地址:http://bootstrap-table.wenzhixin.net.cn/ 另一种是Nuget引入. 然后 ...

  9. PIVOT行转列

    PIVOT通过将表达式某一列中的唯一值转换为输出中的多个列来旋转表值表达式,并在必要时对最终输出中所需的任何其余列值执行聚合. 测试数据 INSERT INTO [TestRows2Columns] ...

  10. poj-2287---Tian Ji -- The Horse Racing

    #include<iostream> #include<algorithm> using namespace std; bool cmp(const int,const int ...