2011-09-03 25 views
1

我想在選擇'option other'單選按鈕時啓用'other text'字段。我可以完成這項工作,但表格必須包含多個無線電組。當我嘗試添加另一個廣播組時,我的代碼會導致兩個文本字段都被啓用。如何使用單選按鈕啓用特定的文本字段?

我該如何一次只做一件這項工作?我看着給課程添加:eq(),但我無法弄清楚如何完成這項工作。任何幫助表示讚賞。

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

回答

0

你應該分配一個不同的識別器這兩個文本框,例如添加一個類名相關的名稱組:

​​

之後,在你的腳本,你可以找出被點擊哪個組和比你可以做你的激活:

$('.radclass.can').click(function(){ 
    var grp = $(this).attr('name'); 
    $('.' + grp).attr({disabled:false,enabled:true}); 
}); 

下面一個完整的例子(link

+0

感謝您的回答,並感謝您的鏈接。這對我來說很好。我仍然試圖找出是否有辦法使用索引而不是其他類來完成此操作。 –

+0

當然是。檢查出來[鏈接](http://jsfiddle.net/yHLRN/1/) – sirLisko

相關問題