首先,一个问题......为什么每个问题都使用一个表格?如果没有理由,您最好放置一个表单并更改每组单选按钮的“名称”值。首先,我已经稍微清理了你的 HTML...
<form id="questions">
<p class="question-title">Question one...</p>
<input name="firstq" type="radio" data-weight="1" />Yes
<input name="firstq" type="radio" data-weight="-1" />No
<input name="firstq" type="radio" data-weight="0" />Unsure
<p class="question-title">Question two...</p>
<input name="secondq" type="radio" data-weight="1" />Yes
<input name="secondq" type="radio" data-weight="-1" />No
<input name="secondq" type="radio" data-weight="0" />Unsure
<p class="question-title">Question three...</p>
<input name="thirdq" type="radio" data-weight="1" />Yes
<input name="thirdq" type="radio" data-weight="-1" />No
<input name="thirdq" type="radio" data-weight="0" />Unsure
</form>
<div id="show-me-1" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'Yes'</div>
<div id="show-me-2" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are a combination of 'Yes' 'No' and 'Unsure'</div>
<div id="show-me-3" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'No'</div>
解决您的问题的一种可能方法是为每个回答设置一个特定的权重,这样当所有问题都得到回答后,您可以将结果相加,并知道是否所有问题都是“是”、“全部”是“否”或混合。您可以使用单选按钮的“值”属性设置权重,但我更喜欢创建一个独立的数据字段,因此您可以将“值”用于与表单一起发送的某些信息。
所以在这里你有一个可行的方法来做到这一点......
$(document).ready(function() {
$('input[type="radio"]').click(function() {
var nQuestions = $('p.question-title').length;
var $checkedItems = $('input[type="radio"]:checked');
if($checkedItems.length == nQuestions) { // All answered
var sum = 0;
$checkedItems.each(function() {
sum += Number($(this).data('weight'));
});
$('div.result').hide();
if(sum == nQuestions)
$('div#show-me-1').show();
else if(sum == -nQuestions)
$('div#show-me-3').show();
else
$('div#show-me-2').show();
}
});
});
我假设每个问题都会有一个“p”标题,如您所见,我使用该标题来了解问题的数量。您可以根据需要进行调整。
这里有工作小提琴:https://fiddle.jshell.net/rigobauer/m9xnng2e/
已编辑:
显然,您需要在表单中包含不同组问题的这种行为。这里有“扩展”版本:
<form id="questions">
<fieldset>
<p class="question-title">Question one...</p>
<input name="firstq" type="radio" data-weight="1" />Yes
<input name="firstq" type="radio" data-weight="-1" />No
<input name="firstq" type="radio" data-weight="0" />Unsure
<p class="question-title">Question two...</p>
<input name="secondq" type="radio" data-weight="1" />Yes
<input name="secondq" type="radio" data-weight="-1" />No
<input name="secondq" type="radio" data-weight="0" />Unsure
<p class="question-title">Question three...</p>
<input name="thirdq" type="radio" data-weight="1" />Yes
<input name="thirdq" type="radio" data-weight="-1" />No
<input name="thirdq" type="radio" data-weight="0" />Unsure
<div class="show-me-1" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'Yes'</div>
<div class="show-me-2" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are a combination of 'Yes' 'No' and 'Unsure'</div>
<div class="show-me-3" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'No'</div>
</fieldset>
<fieldset>
<p class="question-title">Question one...</p>
<input name="firstq" type="radio" data-weight="1" />Yes
<input name="firstq" type="radio" data-weight="-1" />No
<input name="firstq" type="radio" data-weight="0" />Unsure
<p class="question-title">Question two...</p>
<input name="secondq" type="radio" data-weight="1" />Yes
<input name="secondq" type="radio" data-weight="-1" />No
<input name="secondq" type="radio" data-weight="0" />Unsure
<div class="show-me-1" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'Yes'</div>
<div class="show-me-2" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are a combination of 'Yes' 'No' and 'Unsure'</div>
<div class="show-me-3" class="result" style="display:none; box-sizing: border-box; padding: 20px; background-color: #efefef;">Show this if all answers are 'No'</div>
</fieldset>
</form>
重要提示:请注意,现在显示 div 有“class”,而不是“id”。你不应该在不同的元素中有相同的“id”!!
然后只是 Javascript/jQuery 中的一些更改...
$(document).ready(function() {
$('input[type="radio"]').click(function() {
var $thisFieldset = $(this).closest('fieldset');
var nQuestions = $thisFieldset.find('p.question-title').length;
var $checkedItems = $thisFieldset.find('input[type="radio"]:checked');
if($checkedItems.length == nQuestions) { // All answered
var sum = 0;
$checkedItems.each(function() {
sum += Number($(this).data('weight'));
});
$thisFieldset.find('div.result').hide();
if(sum == nQuestions)
$thisFieldset.find('div.show-me-1').show();
else if(sum == -nQuestions)
$thisFieldset.find('div.show-me-3').show();
else
$thisFieldset.find('div.show-me-2').show();
}
});
});
希望对你有帮助