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

发布于 2024-08-06 11:02:18 字数 364 浏览 2 评论 0原文

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

  • 每当用户单击单选按钮时,文本框都应启用
  • ,而当用户单击其他单选按钮时,文本框应再次禁用。

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

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

我的问题:

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

问候

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

  • 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?

regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

∞琼窗梦回ˉ 2024-08-13 11:02:18

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

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

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

$('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">

有道理吗?

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.

$('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?

南街九尾狐 2024-08-13 11:02:18
$("#some_other_radiobutton").click(function(){
  $("#otherDevText").attr("disabled","disabled");
});
$("#some_other_radiobutton").click(function(){
  $("#otherDevText").attr("disabled","disabled");
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文