簡體   English   中英

嘗試啟用/禁用復選框上的文本框單擊

[英]Trying to enable/disable a textfield on checkbox click

我正在嘗試編寫一個文本字段,該文本字段在“選中”復選框時被禁用。

下面是我正在使用的代碼。 我不知道為什么它不起作用。 我懷疑這可能是WordPress的錯,但是我是Java語言的新手,所以我希望是這樣。

<script type="text/javascript">
$('input[name=AddressCheck]').change(function(){
    if($(this).is(':checked')) {
        $("#dbltext").removeAttr('disabled');
    }
    else{
       $("#dbltext").attr('disabled','disabled');
    }    

});
</script>



<input name="AddressCheck" type="checkbox" id="AddressCheck" /><br />
<input type="text" id="dbltext" disabled/>
$(document).ready(function(){
     $('input[name=AddressCheck]').click(function(){
        if($(this).is(':checked')) { 
            $("#dbltext").removeAttr('disabled');
        } 
});

該腳本在瀏覽器看到頁面的其余部分之前運行,因此永遠找不到該元素,因此不會綁定任何內容

嘗試

$(document).ready(init);
// or document.addEventListener('DOMContentLoaded', init);
function init(){
    $('input[name=AddressCheck]').change(function(){
    if($(this).is(':checked')) {
      $("#dbltext").removeAttr('disabled');
    }
    else{
      $("#dbltext").attr('disabled','disabled');
    }
    });
}

或將腳本移到正文的末尾

暫無
暫無

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

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