天天看點

label通過for屬性和radio,checkbox關聯的checked值變化

點選連結加入群聊【IT學習技術交流會】:https://jq.qq.com/?_wv=1027&k=sJTOsjXG

通過for屬性的label标簽修改樣式不會立即修改radio和checkbox的checked屬性值,需要在radio和checkbox的表單元素上添加新的觸法事件來修改checked屬性值。

$(".requiredNotNull").click(function () {
    var type=$(this).attr("type");
    if(type == "radio"){
        if($(this).attr("checked")){

        }else{
            var name=$(this).attr("name");
            var inputList = document.getElementsByName(name);
            for(var i=0;i<inputList.length;i++){
                $(inputList[i]).attr("checked",false);
            }
            $(this).attr("checked",true);
        }
        console.log($(this).attr("checked"));
    }else if(type == "checkbox"){
        if($(this).attr("checked")){
            $(this).attr("checked",false);
        }else{
            $(this).attr("checked",true);
        }
        console.log($(this).attr("checked"));
    }

});
           
<input type="checkbox" class="gcs-checkbox requiredNotNull" id="${varNum.count }${bean.questionsId }" name="${bean.questionsId }" value="${choice }">
<label for="${varNum.count }${bean.questionsId }"></label>
           

繼續閱讀