簡體   English   中英

如果選中了復選框,則刪除屬性?

[英]If Checkbox is checked, remove attribute?

<div class="span1">
     <input type="checkbox" value="l4" id="l4" field="" defaultValue="" appEditor="true"/>
</div>

<div class="span7">
     <input type="text" class="m-wrap span10" id="fld_l4" defaultValue="" editType="intEdit" appEditor="true" disabled />
</div>

我想做的是,如果選中了此復選框,請在fld_l4中刪除禁用的功能。

如何使用Prototype.js或jQuery做到這一點?

編輯:我正在使用jQuery的原型,我得到一個錯誤: Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable. 順便說一下,我用jQuery替換$來解決沖突

EDIT2:已解決。

this.l4     = editor.instance;
editor.observe(iconstants.KEY_CHANGE,this.levelCheckboxChanged.bindAsEventListener(this))

內部levelCheckboxChanged:

levelCheckboxChanged: function(e) {

    if($("l4").checked == false) {
        $("fld_l4").disabled = true;
    } else {
        $("fld_l4").disabled = false;
    }
},

這種方法應該起作用:

$("#l4").on("change", function () {
    $("#fld_l4").prop("disabled", !$(this).is(":checked"));
});

jsFiddle: http : //jsfiddle.net/J5Nt2/1/

你可以這樣

$('#l4').change(function(){
if(this.checked){
$("#fld_l4").removeAttr('disabled');
 }    
})

演示鏈接

嘗試這個,

if( $("#l4:checked").length ) {
    $("#fld_l4").removeAttr("disabled");
}

這是使用PrototypeJS做到這一點的方法

在您的DOM加載事件中

document.observe('dom:loaded',function(){

    $('l4').observe('click',function(){
        $('fld_l4').writeAttribute('disabled',this.checked);
    });

});

另外,您可能想要更改“ 14”瀏覽器的ID,這與您不希望您使用數字開頭的ID一樣-它們允許您這樣做,但它不是HTML規范的一部分

嘗試這個:

if( $("#l4").is(":checked") ) {  
   $('#fld_l4').removeAttr('disabled');
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM