jQuery 实现单选按钮(radio)勾选和取消,使用prop()代替attr() 踩坑博客
2017-11-01 11:58
791 查看
欢迎来到Altaba的博客 2017年11月1日
很多时候,我们需要原因js脚本去操控一排单选按钮,获取后台数据显示当前项的某个设置值,通过单选框显示,进而还能够通过修改勾选单选按钮去修改这个项的值,由于整个项目前端是通过jQuery实现,果断想到使用attr()方法去实现修改单选按钮checked属性,谁知道遇到一系列无法想象的奇怪问题
比如:手动修改单选按钮,后无法通过jQuery的attr()清除,想办法清除完了后面又无法通过脚本重新勾选单选按钮,就这样测试花了我一下午时间,baidu google一下午都是说用attr()去实现,故没怀疑这个方法。
后面通过查阅资料发现:jquery中同时提供了attr()与prop()方法对属性进行获取,但是还是有一定的区别
原文这样写的:
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6 , the .attr() method sometimes took property values into account when retrieving some attributes, which could
cause inconsistent behavior. As of jQuery 1.6 , the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6 , these properties were retrievable with
the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.
大体上的意思是:在jquery1.6之后,对于checked,selected等进行状态改变时,需要使用的是prop()而不是attr(),判断到底单选框是否选中了,这个也同样是使用prop().!!!
于是修改所有attr()为prop()后完美实现,果然框架的吭能把程序员逼疯
下面是demo代码,欢迎大家复制测试
很多时候,我们需要原因js脚本去操控一排单选按钮,获取后台数据显示当前项的某个设置值,通过单选框显示,进而还能够通过修改勾选单选按钮去修改这个项的值,由于整个项目前端是通过jQuery实现,果断想到使用attr()方法去实现修改单选按钮checked属性,谁知道遇到一系列无法想象的奇怪问题
比如:手动修改单选按钮,后无法通过jQuery的attr()清除,想办法清除完了后面又无法通过脚本重新勾选单选按钮,就这样测试花了我一下午时间,baidu google一下午都是说用attr()去实现,故没怀疑这个方法。
后面通过查阅资料发现:jquery中同时提供了attr()与prop()方法对属性进行获取,但是还是有一定的区别
原文这样写的:
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6 , the .attr() method sometimes took property values into account when retrieving some attributes, which could
cause inconsistent behavior. As of jQuery 1.6 , the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.
For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should be retrieved and set with the .prop() method. Prior to jQuery 1.6 , these properties were retrievable with
the .attr() method, but this was not within the scope of attr. These do not have corresponding attributes and are only properties.
大体上的意思是:在jquery1.6之后,对于checked,selected等进行状态改变时,需要使用的是prop()而不是attr(),判断到底单选框是否选中了,这个也同样是使用prop().!!!
于是修改所有attr()为prop()后完美实现,果然框架的吭能把程序员逼疯
下面是demo代码,欢迎大家复制测试
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <input type="radio" name="aaa" value="小米" tag="0"><br> <input type="radio" name="aaa" value="小是" tag="0"><br> <input type="radio" name="aaa" value="小谁" tag="0"><br> <input type="radio" name="aaa" value="小发" tag="0"><br> <input type="radio" name="aaa" value="小哦" tag="0"> <button class="btn1">点我除去选中项</button> <button class="btn2">点我选中第二个</button> <button class="btn3">点我选中第三个</button> <button class="btn4">点我选中第四个</button> <button class="btn5">点我选中第五个</button> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script> /*//解决选中的单选框无法取消问题 $(":radio").click( function(){ var nm=$(this).attr("name"); $(":radio[name="+nm+"]:not(:checked)").attr("tag",0); if($(this).attr("tag")==1){ $(this).attr("checked",false); $(this).attr("tag",0); } else{ $(this).attr("tag",1); } } );*/ function de() { //错误示范,均会出现什么各种问题 /*$.each($('input:radio'),function(i,v){ $(v).attr('checked', false); $(v).removeAttr('checked'); //v.checked = false; //v.removeAttribute("checked"); })*/ //$("input[name=aaa]").prop("checked",false); $('input:checked').prop('checked', false); //$("input[name=aaa]").removeAttr("checked") } $('.btn1').click(function () { de() }) $('.btn2').click(function () { //de(); $('input:radio').eq(1).prop('checked', true); }) $('.btn3').click(function () { //de(); $('input:radio').eq(2).prop('checked', true); }) $('.btn4').click(function () { //de(); $('input:radio').eq(3).prop('checked', true); }) $('.btn5').click(function () { //de(); $('input:radio').eq(4).prop('checked', true); }) </script> </body> </html>
相关文章推荐
- 在jquery用一个按钮实现全选/全不选的效果(attr和prop的区别使用)
- Jquery中对checkbox的各种“全选”或者“取消”功能实现(特别注意1.6+的一定不能使用attr来取属性了!用prop!)
- jquery中radio,CheckBox使用attr和prop的区别
- jQuery实现radio的单选,和取消单选
- jquery全选/取消全选(反选)/单选操作以及获取值常见用法和错误-jquery prop()函数使用教程
- jquery实现radio按钮在分组状态下点击选中,再次点击取消选中
- jquery实现单选按钮选中与取消选中
- 使用jQuery获取单选按钮radio的选中值
- jQuery使用scrollTop获取div标签的滚动条已滚动高度(jQuery版本1.6+时,用prop()方法代替attr()方法)
- Jquery操作单选按钮(Radio)的取值赋值实现代码
- 使用jquery获取单选按钮radio的值
- html中点击radio后的文字相当于选中该单选按钮如何实现?怎么实现两个redio,选中一个,取消另一个?
- 使用jquery实现点击按钮弹出层和点击空白处隐藏层
- jquery 获取自定义属性(attr和prop)的实现代码
- input 单选按钮radio 取消选中
- jquery:使用按钮对table实现分页
- RadioGroup、RadioButton(单选按钮)、CheckBox(多选按钮)和Toast的使用方法
- Struts2单选按钮标签s:radio的使用及其设置默认值
- jQuery实现简单的导航按钮焦点背景更换(代替css hover)以及弹出子菜单 我的第一篇博文
- input 单选按钮radio 取消选中