这个想法是,您想使用 JavaScript 根据上一个问题的答案来显示/隐藏问题。这称为决策树。如果你谷歌它,他们会来的。您可以找到一堆示例和库来为您完成大部分繁重的工作。
如果你想建立自己的,这里有一种非常简单的方法。这不是一个可扩展的解决方案,但它应该让您了解它应该如何工作的基本概念。
HTML
<label>Do you want this?
<select name="choices" id="choices">
<option value="1">Yes</option>
<option value="2">No</option>
<option value="3" selected>Nothing</option>
</select>
</label>
<div id="choices-followup">
<div id="followup1">
<label>
How bad do you want this?
<select>
<option>Give it to me now!</option>
<option>Meh...</option>
</select>
</label>
</div>
<div id="followup2">
<label>Why not?<br />
<textarea></textarea>
</label>
</div>
</div>
JavaScript
// Whenever the user changes the value of
// the element with ID "choices", perform
// this function.
$('#choices').on('change', function() {
// Grab the choice value
var choice = $(this).val();
// Cache the "choices-followup" object.
// Every time to you call $('anything'), jQuery
// goes through the entire DOM looking for that
// object. Prevent this by making the lookup
// once, and caching the value.
var choices_followup = $('#choices-followup');
// No matter what the choice, hide all possible
// follup fields.
$('div', choices_followup).hide();
// Also, reset their values.
$('select, textarea, input', choices_followup).val('');
// If this, then that, etc.
switch(choice) {
case '1':
$('#followup1').show();
break;
case '2':
$('#followup2').show();
break;
}
});
CSS
#choices-followup > div { display: none; }