####全选【attr(“checked”,true)】;全不选【必须用removeAttr(“checked”),不能用attr("checked",false)》这家伙或有“第二次全选失败”问题】

####全选【attr(“checked”,true)】;全不选【必须用removeAttr(“checked”),不能用attr("checked",false)》这家伙或有“第二次全选失败”问题】

====》用的jquery。

====》第二次全选失败”问题,找到这个:https://blog.csdn.net/qq_23313625/article/details/68938655

试了下prop,没卵用。


【小结:

①“能不用prop就不用prop!!!”。

②attr(“checked”),要想失效,必须removeAttr(“checked”)


=====》费了挺久一段时间。Mark一下【最终可行的例子。】,警示。

改来改去,看着心酸。。。

// jquery全选+全不选 流量配置
function checkAll(){
    var checkboxTitle = $("#checkboxRuleTitle");
    var checkboxBody = $("input[type='checkbox'][name='checkboxRuleBody']");
    var titleIsCheck = checkboxTitle.attr("checked");
    alert("titleIsCheck:"+titleIsCheck);
    // if(titleIsCheck == false){ //勾选 。prop。需要手动赋值???
    if(titleIsCheck == undefined){ //勾选 。prop。需要手动赋值???
        // checkboxTitle.prop("checked",true);
        checkboxTitle.attr("checked",true);//两个属性一起用。。。
        //全选body。
        checkboxBody.each(function () {
            // $(this).prop("checked",true);//只写这一句。只有第一次有效。。。???
            var bodyThisIsCheck = $(this).attr("checked");
            // alert("bodyThisIsCheck:"+bodyThisIsCheck);

//方式1:
            $(this).prop("checked",true);//只写这一句。只有第一次有效。。。???
           /* //方式2:判断。。。
            // if(bodyThisIsCheck == false){ //勾选
            if(bodyThisIsCheck == undefined){ //勾选
                // $(this).prop("checked",true);//先手动修改body 属性值。便于后面判断。【第二次全选,不生效。代码走了。】
                $(this).attr("checked",true);
            // }else if(bodyThisIsCheck == true){ //取消勾选。
            }else if(bodyThisIsCheck.indexOf("checked")!=-1){ //取消勾选。
                // $(this).prop("checked",false);
                // $(this).attr("checked",false);
                $(this).removeAttr("checked");
            }*/

        })
    // }else if(titleIsCheck == true){ //取消勾选。
    }else if(titleIsCheck.indexOf("checked")!=-1){ //取消勾选。
        // checkboxTitle.prop("checked",false);
        // checkboxTitle.attr("checked",false);
        checkboxTitle.removeAttr("checked");
        //全不选body
        checkboxBody.each(function () {
            // $(this).prop("checked",false);//只写这一句。只有第一次有效。。。???
            // $(this).attr("checked",false);//只写这一句。只有第一次有效。。。???
            $(this).removeAttr("checked");
           /*
           //方式2:判断。。。
           // var bodyThisIsCheckAttr = $(this).prop("checked");//#########prop有属性就生效。导致Title总是true。  必须用Attr???
            var bodyThisIsCheck = $(this).attr("checked");//有属性就生效。导致Title总是true。
            alert("bodyThisIsCheck:"+bodyThisIsCheck);
            // if(bodyThisIsCheck == false){ //勾选
            if(bodyThisIsCheck == undefined){ //勾选
                // $(this).prop("checked",true);//先手动修改body 属性值。便于后面判断。
                $(this).attr("checked",true);//先手动修改body 属性值。便于后面判断。
            // }else if(bodyThisIsCheck == true){ //取消勾选。
            }else if(bodyThisIsCheck.indexOf("checked")!=-1){ //取消勾选。
                // $(this).prop("checked",false);
                // $(this).attr("checked",false);
                $(this).removeAttr("checked");
            }*/
        })
    }

    //①全是Attr,只有第一次有效。
    //②全是prop,全选,全不选可以,Title总是true。不能同步 一致。
    //③全是prop,拿到属性值处用attr,总是全选状态。不能全不选。。。
    //④全是先prop后attr,??
}


====》补充:问题所在找到了:

function checkAll() {
    var checkboxTitle = $("#checkboxRuleTitle");
    var checkboxBody = $("input[type='checkbox'][name='checkboxRuleBody']");
    var titleIsCheck = checkboxTitle.attr("checked");
    alert("titleIsCheck:" + titleIsCheck);
    if (titleIsCheck == undefined) { //勾选 。prop。需要手动赋值???
        checkboxTitle.attr("checked", true);//两个属性一起用。。。
        //全选body。
        checkboxBody.each(function () {
            var bodyThisIsCheck = $(this).attr("checked");
            $(this).prop("checked", true);//【###】这一句。换成 attr就不行了。。。【第二次全选就会失败!!!】
            // $(this).attr("checked", true);//【###】这一句。换成 attr就不行了。。。【第二次全选就会失败!!!】
            // 》》》》》》【问题所在。。。找到了。。。】===》【要想“反复全选 子CheckBox”成功。 必须用prop,不能用attr】


        })
    } else if (titleIsCheck.indexOf("checked") != -1) { //取消勾选。
        checkboxTitle.removeAttr("checked");
        //全不选body
        checkboxBody.each(function () {
            $(this).removeAttr("checked");
        })
    }
}








猜你喜欢

转载自blog.csdn.net/qq_20597149/article/details/80834064