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. zTree新增的根结点再新增子节点reAsyncChildNodes不生效解决方案

    zTree新增的根结点再新增子节点reAsyncChildNodes不生效解决方案, zTree新的根结点不能异步刷新,reAsyncChildNodes不生效解决方案, reAsyncChildNo ...

  2. 两百条微信小程序跳坑指南(不定时更新)

    微信小程序联盟出品 跳坑textarea<二百二十三>不显示文本及textarea相关问题集合跳坑<二百一十三> background-image无法获取本地资源图片....跳 ...

  3. JavaScript addEventListener 第三个参数

    先看一个完整的演示页面代码. Code <!DOCTYPE html> <html lang="zh-CN"> <head> <meta ...

  4. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  5. git分支管理之多人协作

    当你从远程仓库克隆时,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且,远程仓库的默认名称是origin. 要查看远程库的信息,用git remote: $ git r ...

  6. python pygame--倒计时

    import pygame,sys,time,datetime class decTime(object): #将秒转化为时分秒 def __init__(self,totalTime): self. ...

  7. Fluent Validation with Web Api 2

    using FluentValidation;using FluentValidation.Attributes;using System;using System.Collections.Gener ...

  8. connect by prior 递归算法

    http://blog.163.com/xxciof/blog/static/7978132720095193113752/ oracle中 connect by prior 递归算法 Oracle中 ...

  9. AWK求和、平均值、最值

    --AWK求和.平均值.最值------------------------2014/02/14  打包当前目录下的所有文件 ls | awk '{ print "tar zcvf &quo ...

  10. 熟悉JS中的常用选择器及属性、方法的调用

    选择器.属性及方法调用的配合使用: <style>            #a{                width: 200px;                height: 1 ...