簡體   English   中英

復選框狀態更改不起作用

[英]Checkbox state change not working

在下面的代碼中,首先取消選中復選框,然后禁用文本框。當我單擊復選框,然后在再次啟用未選中復選框的情況下啟用文本框后啟用文本框,但是需要禁用它。但是該部分不起作用(其他部分)

 $(function(){
    $("#txtres").attr("disabled", "disabled");
    $("#txtres2").attr("disabled", "disabled");

    $("#chknew").click(function () {
        if("#chknew").is(:checked)
        {
        $("#txtres").removeAttr("disabled");
        $("#txtres2").removeAttr("disabled");
        }
      else
      {
         $("#txtres").attr("disabled", "disabled");
         $("#txtres2").attr("disabled", "disabled");
        }
    });
    }

您可以使用“更改”處理程序來實現

$(function(){
   $("#txtres").attr("disabled", "disabled");
   $("#txtres2").attr("disabled", "disabled");

$('#chknew').change(function () {
   if ($(this).is(":checked")) {
       $("#txtres").removeAttr("disabled");
       $("#txtres2").removeAttr("disabled");
   } else {
       $("#txtres").attr("disabled", "disabled");
       $("#txtres2").attr("disabled", "disabled");
   }
  });
});

要再次禁用它,請像波紋管一樣說

$("#txtres").prop("disabled", true);

代替

$("#txtres").attr("disabled", "disabled");
$(function(){
    $("#txtres").attr("disabled", "disabled");
    $("#txtres2").attr("disabled", "disabled"); 
    $("#chknew").click(function () {    
        $("#txtres").attr("disabled", !this.checked);
        $("#txtres2").attr("disabled", !this.checked);          
    });
});

演示:

http://jsfiddle.net/w2jtg1b9/3/

暫無
暫無

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

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