jQuery 实现单选按钮(radio)勾选和取消,使用prop()代替attr() 踩坑博客

欢迎来到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代码,欢迎大家复制测试

<!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>


  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
取消选中radio单选按钮,可以通过以下几种方式实现: 1. 使用JavaScript: 可以通过JavaScript代码来取消选中radio单选按钮。首先,需要获取到该radio单选按钮的DOM元素,可以通过id、class或者其他属性来选取。然后,使用`checked`属性将其设为false即可取消选中。示例代码如下: ``` document.getElementById("radioId").checked = false; ``` 这里的"radioId"是具体radio单选按钮的id。 2. 使用jQuery: 如果网页中引入了jQuery库,可以使用更简洁的代码来取消选中radio单选按钮。首先,需要获取到该radio单选按钮jQuery对象,可以通过id、class或者其他选择器来选取。然后,使用`prop()`方法将其选中属性设为false即可取消选中。示例代码如下: ``` $("#radioId").prop("checked", false); ``` 这里的"radioId"是具体radio单选按钮的id。 3. 利用HTML的`<form>`表单: 在HTML的`<form>`标签中,可以设定同一个name属性的radio单选按钮为同一组。当其中一个radio单选按钮被选中时,其他的单选按钮会被自动取消选中。因此,可以通过设置其他radio单选按钮为选中状态来达到取消选中的效果。示例代码如下: ``` <form> <input type="radio" name="radioGroup" value="1" checked>选项1<br> <input type="radio" name="radioGroup" value="2">选项2<br> <input type="radio" name="radioGroup" value="3">选项3<br> </form> ``` 以上代码中,设定了name属性为"radioGroup"的三个radio单选按钮为同一组。当选中其中一个按钮时,其他按钮会自动取消选中。 总之,通过JavaScript、jQuery或者HTML的`<form>`表单,可以很方便地取消radio单选按钮的选中状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值