jquery enable/disable text box based on radiobutton

眉间皱痕 提交于 2019-12-21 07:23:13

问题


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


回答1:


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?




回答2:


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


来源:https://stackoverflow.com/questions/1531747/jquery-enable-disable-text-box-based-on-radiobutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!