我有一個單選按鈕組的調查形式,其HTML看起來像:jQuery的切換DIV基於單選按鈕選擇
<label>Would you like to compete?</label>
<input type="radio" name="compete" value="2" id="competenoradio" >
<label for="compete_no">No</label>
<input type="radio" name="compete" value="1" id="competeyesradio">
<label for="compete_yes">Yes</label>
<div id="competeyes">
<label class="competeyestxt" hidden=true>Which Location?</label>
<textarea rows="4" cols="40" maxlength="2000" name="competeyestextarea" class="competeyestxt" hidden="true">
</textarea>
</div>
The div at the bottom with id 'competeyes' need to be toggled between invisible to visible
based on whether the user selected radio button with id 'compete_yes'.
這是我在它的企圖,這是行不通的。
Attempt 1:
$('#competeyesradio').bind('change',function(){
$('#competeyes').toggle($(this).is(':checked'));
});
Attempt 2:
$('div.input input:radio').bind('change',function(){
$('#competeyes').toggle($(this).is(':checked'));
});
Attempt 3:
$('[name=compete]').bind('change',function(){
$('#competeyes').toggle($(this).is(':checked'));
});
有什麼建議嗎?
只是'var showOrHide = $(this).val()== 1;'也很好。 – Qtax
@Qtax:這會做到這一點,但我不希望OP對代碼感到驚訝。 – Shef
在目前的情況下,$(this)是什麼?我認爲在語句$('#conteyes')。toggle($(this).is(':checked'));}); $(this)將是#competeyes。沒有? – truthSeekr