jquery启用/禁用基于radiobutton的文本框

13 checkbox jquery radio-button

在我的页面(jsp)中,我有一个radiobutton组和一个文本框(最初被禁用).

  • 每当用户点击单选按钮时,应启用文本框
  • 当用户点击其他一些单选按钮时,文本框应再次被禁用.

我可以使用下面的代码启用最初禁用的复选框.

$("#DevGroup_OTHER").click(function(){          
    $("#otherDevText").attr("disabled","");
})
Run Code Online (Sandbox Code Playgroud)

我的问题:

  • 但是我怎么能再次禁用文本框?
  • 使用jQuery有更简单的解决方案吗?

问候

Kyl*_*ris 33

始终禁用它(对于每个单选按钮),如果单选按钮是启用文本框的单选按钮,则重新启用它.除非用户使用1980年制造的机器,否则它将如此之快,而不是人们所知道的.

$('radio').click(function() { 
    $("#otherDevText").prop("disabled",true);
    if($(this).attr('id') == 'enable_textbox') {
        $("#otherDevText").prop("disabled",false);
    }
});
Run Code Online (Sandbox Code Playgroud)

或者,如果有多个单选按钮可以启用文本框:

$('input:radio').click(function() { 
  $("#otherDevText").prop("disabled",true);
  if($(this).hasClass('enable_tb')) {
      $("#otherDevText").prop("disabled",false);
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="DevGroup_OTHER">
  <input type="radio" id="d1" name="r1" value="d1">dev1 <br /> 
  <input type="radio" id="d2" name="r1" value="d2">dev2 <br/> 
  <input type="radio" id="d3" name="r1" value="d3" class="enable_tb"> enable
</fieldset>
<br />
<input id="otherDevText" name="tb1" disabled="disabled"
       value="some Textbox value" type="text">
Run Code Online (Sandbox Code Playgroud)

合理?


And*_*ell 7

$("#some_other_radiobutton").click(function(){
  $("#otherDevText").attr("disabled","disabled");
});
Run Code Online (Sandbox Code Playgroud)