jquery实现radio按钮在分组状态下点击选中,再次点击取消选中

效果实现共需三个文件:

1、test.html    内容:

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script type="text/javascript" src="./jquery1.8.2.js"></script>
    <script type="text/javascript" src="./radioChecked.js"></script>
</head>
<body>
    <!-- <input type="radio" name="test" value="a" data-check="1">
    <input type="radio" name="test" value="b" data-check="1">

    <HR>
    <input type="radio" name="test1" value="c" data-check="1">
    <input type="radio" name="test1" value="d" data-check="1">
    <input type="radio" name="test1" value="e" data-check="1"> -->
    <input type="radio" name="test" value="a">
    <input type="radio" name="test" value="b">

    <HR>
    <input type="radio" name="test1" value="c">
    <input type="radio" name="test1" value="d">
    <input type="radio" name="test1" value="e">
</body>
<script>

$(function(){
    
    $('.testch').click(function(){
        $(this).radioChecked().init();
    });
    $('.testch').radioChecked().setDataAttr()
});
</script>
</html>

2、radioChecked.js     内容

    ;(function($, window, document, undefined){
    var MyPlugin = function(obj){
        this.$object = obj,
        this.className = this.getClass(),
        this.elementName = this.getElementName()
        //this.options = $.extend({}, this.defaults, opt)
    }

    MyPlugin.prototype = {
        init: function() {
            if(this.$object.data('check') == '1'){
                $(':radio[name="'+ this.elementName +'"]').each(function(){
                    $(this).data('check','1');
                });
                this.$object.data('check','2');
                alert('选中');
            }
            else if(this.$object.data('check') == '2'){
                this.$object.data('check','1');
                this.$object.removeAttr('checked');
                alert('取消选中');
            }
        },
        getClass: function(){
            return this.$object.attr('class');
        },
        getElementName: function(){
            return this.$object.attr('name');
        },
        setDataAttr: function(){
            $('.'+this.className).data('check','1');
        }
    }
    
    $.fn.radioChecked = function(){
        return new MyPlugin(this);
    }
})(jQuery, window, document);

3、jquery1.8.2.js

转载于:https://my.oschina.net/u/2480675/blog/529726

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
取消选中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单选按钮为同一组。当选中其中一个按钮时,其他按钮会自动取消选中。 总之,通过JavaScriptjQuery或者HTML的`<form>`表单,可以很方便地取消radio单选按钮选中状态
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值