我正在尝试根据特定条件调用3种不同的窗口警报。在我的if-else语句中,有以下内容(仅摘要):
$('input[name=answer]').on('change', function() {
if ((practice_trials.stimulus == "images/some-bias1.png" || practice_trials.stimulus == "images/some-unbias1.png" || practice_trials.stimulus == "images/all-unbias3.png") && $('input[id=no]:radio').is(':checked')){
window.alert("Oops, you should have taken the box!");
} else if (practice_trials.stimulus == 'images/all-false3.png' && $('input[id=yes]:radio').is(':checked')){
window.alert("Oops, you should have left the box behind!");
} else {
window.alert("You've got it right!");
}
jQuery指向单选按钮选择,而JS则指向存储在名为 practice_trials.js 的单独文件中的字典。字典具有以下结构:
var practice_trials = [
{question: "Should you bring Johnny this box or not?",
QUD: "Johnny says: 'I want you to bring me the box where ...",
sentence: "some | of the | black marbles | are | inside | the case.'",
option1: "Bring it",
option2: "Leave it",
stimulus: "images/some-bias1.png",
helpText: "Press the SPACE bar to reveal the words"},
HTML具有以下结构:
<p class="answer-container nodisplay">
<label for="yes" class="button-answer">{{option1}}</label>
<input type="radio" name="answer" id="yes"
value={{option1}} />
<input type="radio" name="answer" id="no" value={{option2}} />
<label for="no" class="button-answer">{{option2}}</label>
</p>
到目前为止,无论情况如何,我总是会收到最后一个警报(“您做对了!”)。
答案 0 :(得分:0)
看起来您的数据是一个数组。正在使用把手(或类似工具)在字典中循环吗?如果是这样,您将if / else语句放在哪里?
假设您的数据正确循环,这是一个示例,它适用于您提供的1个特定数据集。
这里的技巧主要是关于.on('click')函数的:http://jsfiddle.net/ujpz9sny/
$("input[type='radio']").on('click', function(e) {
// # you'd want your if/else statement here to check for correct answer
})
如果您并非绝对愿意这样做,则还有更好的方法使用form
。