Javascript 如何使用单选按钮启用特定的文本字段?
我想在选择“选项其他”单选按钮时启用“其他文本”字段。我可以做到这一点,但形式必须包括一个以上的广播组。当我尝试添加另一个无线电组时,我的代码会使两个文本字段都启用 我怎样才能一次只为一个人工作?我想在课堂上加上:eq,但我不知道该怎么做。感谢您的帮助Javascript 如何使用单选按钮启用特定的文本字段?,javascript,jquery,Javascript,Jquery,我想在选择“选项其他”单选按钮时启用“其他文本”字段。我可以做到这一点,但形式必须包括一个以上的广播组。当我尝试添加另一个无线电组时,我的代码会使两个文本字段都启用 我怎样才能一次只为一个人工作?我想在课堂上加上:eq,但我不知道该怎么做。感谢您的帮助 <script> $(document).ready(function() { $('.textclass').attr({disabled:true,enabled:false}); $('.radclass.c
<script>
$(document).ready(function() {
$('.textclass').attr({disabled:true,enabled:false});
$('.radclass.can').click(function(){
$('.textclass').attr({disabled:false,enabled:true});
});
$('.radclass.cant').click(function(){
$('.textclass').attr({disabled:true,enabled:false});
});
});
</script>
<form name="form1" method="post" action="next.php">
<input type="radio" name="grp_one" class="radclass cant" value="option_one" />Option One<br />
<input type="radio" name="grp_one" class="radclass cant" value="option_two" />Option Two<br />
<input type="radio" name="grp_one" class="radclass can" value="option_other" />Option Other<br />
Other Text: <input type="text" name="text_one" class="textclass"/>
<br />
<input type="radio" name="grp_two" class="radclass cant" value="option_one" />Option One<br />
<input type="radio" name="grp_two" class="radclass cant" value="option_two" />Option Two<br />
<input type="radio" name="grp_two" class="radclass can" value="option_other" />Option Other<br />
Other Text: <input type="text" name="text_two" class="textclass"/>
<br />
<input type="submit" name="submit" value="submit">
</form>
您应该为这两个文本字段分配不同的标识符,例如在名称组中添加相对类名:
<input type="text" name="text_one" class="textclass grp_one"/>
这里是一个完整的示例感谢您的回答和链接。那对我来说很好。不过,我仍在试图找出是否有一种方法可以使用索引而不是额外的类来实现这一点。
$('.radclass.can').click(function(){
var grp = $(this).attr('name');
$('.' + grp).attr({disabled:false,enabled:true});
});