我有一個簡單的html表單,其中包含一系列表格中的問題。如果用戶回答「是」/「否」單選按鈕問題,則我想顯示一個隱藏行,允許他們在textarea字段中輸入詳細信息。如果他們點擊否,則textarea輸入應該被清除並再次隱藏。根據是/否單選按鈕有條件地顯示/隱藏表格行
這裏是我的HTML表單一個是/否的問題,一個隱藏的行有關更多詳細信息,如果他們單擊是:
<form class="form-horizontal" action="#" method="post" id="questionForm">
<input type="hidden" name="recid" value="1">
<table class="table table-condensed table-hover table-bordered">
<tr>
<td><strong>Question 1</strong></td>
<td>please answer yes or no to this question</td>
<td>
<div class="controls">
<label class="radio inline">
<input type="radio" name="question1" id="question1" value="Yes" required>Yes </label>
<label class="radio inline">
<input type="radio" name="question1" id="question1" value="No" required>No </label>
<label for="question1" class="error"></label>
</div>
</td>
</tr>
<tr class="question1yes">
<td></td>
<td>Please describe this and when it started</td>
<td>
<div class="controls">
<textarea name="question1Details" rows="3"></textarea>
<label for="question1Details" class="error"></label>
</div>
</td>
</tr>
</table>
</div>
<div class="control-group">
<div class="controls">
<button type="submit" class="btn btn-primary">Continue</button>
<button type="reset" class="btn">Reset</button>
</div>
</div>
</form>
這裏是我不工作電流腳本:
$().ready(function() {
// validate the form when it is submitted
$("#questionForm").validate();
if($("#question1:checked").length != 0){
// yes is checked... show the dependent fields
$(".question1yes").show();
}else{
// hide it and blank the fields, just in case they have something in them
$(".question1yes").hide();
$("#question1Details").val("");
}
$("#question1").click(function(){
// show the dependent fields
if(this.value == "Yes"){
$("#question1yes").show();
}else{
// hide the dependent fields and blank them
$(".question1yes").hide();
$("#question1Details").val("");
}
});
});
我已經設置了一個jsFiddle here,它演示了我目前的狀態。我的可選行開始隱藏,但在單擊yes單選按鈕時不會顯示。
對於初學者來說:你有ID值這是不允許相同的兩個元素。 (ID = 「問題1」)。其實這就是爲什麼它不適合你 – Drew
我已經在短短4個月內回答了這麼多次這個問題我加入了這個網站,我什至不能再打擾了。人們應該在發佈前進行搜索 – melancia