jquery 基于单选按钮启用/禁用文本框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1531747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:39:16  来源:igfitidea点击:

jquery enable/disable text box based on radiobutton

jquerycheckboxradio-button

提问by KyleFarris

In my page (jsp) i have a radiobutton group and a textbox (which is disabled initially).

在我的页面 (jsp) 中,我有一个单选按钮组和一个文本框(最初是禁用的)。

  • Whenever the user clicks on a radio button the textbox should be enabled
  • and when the user clicks on some other radio button the textbox should again get disabled.
  • 每当用户单击单选按钮时,应启用文本框
  • 当用户点击其他一些单选按钮时,文本框应该再次被禁用。

I am able to enable the initially disabled checkbox with the code below.

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

$("#DevGroup_OTHER").click(function(){          
    $("#otherDevText").attr("disabled","");
})

My questions:

我的问题:

  • But how can i disable the textbox again?
  • Is there a simpler solution using jQuery?
  • 但是我怎样才能再次禁用文本框?
  • 是否有使用 jQuery 的更简单的解决方案?

regards

问候

回答by KyleFarris

Always disable it (for every radio button), then re-enable it if the radio button is the one that enables the textbox. Unless the user is on a machine built in 1980, it will be so fast, not one will ever know.

始终禁用它(对于每个单选按钮),然后如果单选按钮是启用文本框的那个,则重新启用它。除非用户使用的是 1980 年制造的机器,否则它会如此之快,没有人会知道。

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

Alternatively, if there are multiple radio buttons that will enable the textbox:

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

$('input:radio').click(function() { 
  $("#otherDevText").prop("disabled",true);
  if($(this).hasClass('enable_tb')) {
      $("#otherDevText").prop("disabled",false);
  }
});
<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">

Make sense?

有道理?

回答by Andy Gaskell

$("#some_other_radiobutton").click(function(){
  $("#otherDevText").attr("disabled","disabled");
});