JS radio 取消选择,并触发原选择项改变事件

版权声明:随意乱写,切勿当真! https://blog.csdn.net/pehao/article/details/78910095
 $div.find("input[type='radio']").each(function () {
                    if ($(this).prop("checked") == true) {
                        $(this).attr("data-mutex-check", 'true');
                    } else {
                        $(this).attr("data-mutex-check", 'false');
                    }
                }).click(function () {
                    var check = $(this).attr("data-mutex-check");
                    var name = $(this).attr("name");
                    if (check == "true") {
                        $(this).prop("checked", false).change();                        
                        $(this).attr("data-mutex-check", "false");
                    } else {
                        //找到已经选择的项目
                        $("input[type='radio'][name='" + name + "']").each(function () {

                            if ($(this).attr("data-mutex-check") == 'true') {

                                $(this).attr("data-mutex-check", "false").change();
                            }                            
                        });
                        $(this).attr("data-mutex-check", "true");
                    }
                });

解决思路:

1.通过添加公共属性 data-mutex-check来标记当前radio状态,初始化时全部赋值一次.

2.当其中一个click时, 标记click之前选中项属性为false.当前click项属性true

3.为了触发之前选中项目已经修改.需要添加 change方法. 

猜你喜欢

转载自blog.csdn.net/pehao/article/details/78910095
今日推荐