我这里没有任何形式。只是几个问题。我想在这里检查是否选中了单选按钮,然后将其值存储在数组中并移至下一个屏幕。我发现客户端验证所需的属性仅适用于表单,而我这里没有任何表单。
这是我的HTML下面:
<div class="first-div">
<p>Question1: Where in the world would you find the Spire?</p>
<span><input type="radio" name="radio" id="a1" value="a1" /> Kerry. </input></span>
<span><input type="radio" name="radio" id="a1" value="a1" /> Galway. </input></span>
<span><input type="radio" name="radio" id="a1" value="a1" /> Dublin. </input></span>
<span><input type="radio" name="radio" id="a1" value="a1" /> Donegal. </input></span>
<div class="button_box">
<button class="back" disabled="true" style="color:#aaa">Back</button>
<button class="next">Next</button>
</div>
</div>
<div class="next-div" style="display:none;">
<p>Question2: Where in the world would you find the Colosseum?</p>
<span><input type="radio" name="radio" id="b1" value="a2" /> Taipei. </input></span>
<span><input type="radio" name="radio" id="b1" value="a2" /> Rome. </input></span>
<span><input type="radio" name="radio" id="b1" value="a2" /> Reykjavic. </input></span>
<span><input type="radio" name="radio" id="b1" value="a2" /> Brussels. </input></span>
<div class="button_box">
<button class="back">Back</button>
<button class="next">Next</button>
</div>
</div>
<div class="next-div" style="display:none;">
<p>Question3: Where in the world would you find the Colosseum?</p>
<span><input type="radio" name="radio" id="c1" value="a3" /> Taipei. </input></span>
<span><input type="radio" name="radio" id="c1" value="a3" /> Rome. </input></span>
<span><input type="radio" name="radio" id="c1" value="a3" /> Reykjavic. </input></span>
<span><input type="radio" name="radio" id="c1" value="a3" /> Brussels. </input></span>
<div class="button_box">
<button class="back">Back</button>
<button class="next">Next</button>
</div>
</div>
<div class="next-div" style="display:none;">
<p>Question4: Where in the world would you find the Colosseum?</p>
<span><input type="radio" name="radio" id="d1" value="a3" /> Taipei. </input></span>
<span><input type="radio" name="radio" id="d1" value="a3" /> Rome. </input></span>
<span><input type="radio" name="radio" id="d1" value="a3" /> Reykjavic. </input></span>
<span><input type="radio" name="radio" id="d1" value="a3" /> Brussels. </input></span>
<div class="button_box">
<button class="back">Back</button>
<button class="finish">Finish</button>
</div>
</div>
这是我现在的jQuery
$('.next').click(function(){
$(this).parent().parent().hide().next().show();//hide parent and show next
});
$('.back').click(function(){
$(this).parent().parent().hide().prev().show();//hide parent and show previous
});
最佳答案
检查这将有助于:-
var checkedValues = [];
$('.next').click(function() {
var $form = $(this).parent().parent();
var $checked = $(this).parent().parent().find('input[type=radio]:checked')
var isChecked = $checked.length > 0;
if(!isChecked){
alert('Please Choose an option.');
return;
}
checkedValues.push($checked.val());
console.log($checked.val());
$form.hide().next().show(); //hide parent and show next
});
$('.back').click(function() {
console.log('back');
$(this).parent().parent().hide().prev().show(); //hide parent and show previous
});
这是一个工作的demo
关于javascript - 是否选中了单选按钮,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41712642/