jquery启用/禁用基于单选按钮的文本框 [英] jquery enable/disable text box based on radiobutton
本文介绍了jquery启用/禁用基于单选按钮的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的页面(jsp)中,我有一个radiobutton组和一个文本框(初始禁用)。
- 在单选按钮上,文本框应启用
- ,当用户点击其他单选按钮时,文本框应再次禁用。
我可以用下面的代码启用最初禁用的复选框。
$ #DevGroup_OTHER)。click(function(){
$(#otherDevText)。attr(disabled,);
})
我的问题:
解决方案
始终禁用它(对于每个单选按钮),然后重新启用它,如果单选按钮是启用文本框。除非用户在1980年制造的机器上,否则它会如此之快,不会有人知道。
$('radio')。click(function(){
$(#otherDevText)。prop (disabled,true);
if($(this).attr('id')=='enable_textbox'){
$(#otherDevText)。prop 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 =radioid =d1name =r1value =d1> dev1< br /> < input type =radioid =d2name =r1value =d2> dev2< br /> < input type =radioid =d3name =r1value =d3class =enable_tb> enable< / fieldset>< br />< input id =otherDevTextname =tb1disabled =disabledvalue =some textbox valuetype =text>
有意义吗?
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
解决方案
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?
这篇关于jquery启用/禁用基于单选按钮的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文