jquery选中radio或checkbox的正确姿势
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规范的 getAttribute 和 setAttribute进行获取修改,而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的正确姿势的更多相关文章
- jquery的radio和checkbox的标签的操作集合
jquery的radio和checkbox的标签的操作集合: $("input[name='radio_name'][checked]").val(); //选择被选中Radio的 ...
- JQuery触发radio或checkbox的change事件
在JQuery中,当给radio或checkbox添加一个change事件时,如果它的值发生变化就会触发change事件;本文将详细介绍如何利用JQuery触发Checkbox的change事件需要了 ...
- jquery实用应用之jquery操作radio、checkbox、select
本文收集一些jquery的实用技巧,非常实用的哦,其中对radio.checkbox.select选中与取值的方法. 获取一组radio被选中项的值var item = $('input[@name= ...
- jquery对radio和checkbox的操作
jQuery获取Radio选择的Value值 代码 $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Valu ...
- 《jQuery判断radio、checkbox、select 是否选中和设置选中问题以及获取选中值》总结
<form> <input type="radio" name="gender" id="man" value=" ...
- [转]jQuery操作radio、checkbox、select 集合.
1.radio:单选框 html代码 <input type="radio" name="radio" id="radio1" val ...
- jQuery操作radio、checkbox、select 集合
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- jQuery操作radio、checkbox、select总结
1.radio:单选框 HTML代码: <input type="radio" name="radio" id="radio1" va ...
- JQuery 选中Radio
<tr> <td> <input type="radio" name="rdb" value="启用" che ...
随机推荐
- maven引入已经拥有的jar包
<!-- https://mvnrepository.com/artifact/jfree/jcommon --><dependency> <groupId> ...
- (转)Linux 下 查看以及修改文件权限
场景:Linux环境下远程部署项目,发现因为文件权限问题,不能执行远端的可执行文件.问题还没解决,待议... 1 查看权限 在终端输入: ls -l xxx.xxx (xxx.xxx是文件名) 那么就 ...
- Python学习记录----语法学习
一控制语句 http://blog.csdn.net/lynn_yan/article/details/5464911 if 语句 二 字典详解 http://blog.csdn.net/moodyt ...
- 9.Spark Streaming
Spark Streaming 1 Why Apache Spark 2 关于Apache Spark 3 如何安装Apache Spark 4 Apache Spark的工作原理 5 spark弹性 ...
- 深入浅出数据结构C语言班(11)——简要介绍算法时间复杂度
在接下来的数据结构博文中,我们将会开始接触到一些算法,也就是"解决某个问题的方法",而解决同一个问题总是会存在不同的算法,所以我们需要在不同的算法之中做出抉择,而做出抉择的根据往往 ...
- Uva 10142 Australia Voting
水题 模拟 大意就是模拟一个选举的系统 认真读题,注意细节,耐心调试 #include<cmath> #include<math.h> #include<ctype.h& ...
- DynamicXml
/* var xml = @"<root><books><book is_read=""false""><a ...
- 微信开发获取用户OpenID
第一次开发微信版网页,对最重要的获取微信OpenId,特此记录下来 1.首先得有appid和appsecret . public class WeiXin { public static string ...
- Learn c for the Second day
十六进制对应的二进制码 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...