jquery 基于单选按钮启用/禁用文本框 [英] jquery enable/disable text box based on radiobutton

查看:35
本文介绍了jquery 基于单选按钮启用/禁用文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

我的问题:

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

问候

推荐答案

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

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">

有意义吗?

这篇关于jquery 基于单选按钮启用/禁用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆