[英]Check if all radio buttons selected then
我想檢查是否所有單選按鈕都已選中,如果沒有使用警告消息。 警報消息有效,但無論如何都將發送到Answers.php而不選擇字段。 如果警報為真,如何執行表單操作將不起作用? 謝謝。
count.js
$(document).ready(function () {
var names = {};
$(':radio').each(function () {
names[$(this).attr('name')] = true;
});
var count = 0;
$.each(names, function () {
count++;
});
$("#qSubmit").click(function () {
if ($(':radio:checked').length !== count) {
alert("not all checked");
}
});
});
form.php
$(document).ready(function () {
var names = {};
$(':radio').each(function () {
names[$(this).attr('name')] = true;
});
var count = 0;
$.each(names, function () {
count++;
});
$("#qSubmit").click(function () {
if ($(':radio:checked').length !== count) {
alert("not all checked");
}
});
});
<form action="bbb.php" method="post" id="quiz" style="margin-top:100px;" >
<?php
while ($row = $result2->fetch_assoc()) {
echo '<div class="row">';
echo '<div class="col-md-8 col-sm-8 col-xs-12">';
echo '<div class="borderis">' . $row['question'] . '</div><br>';
$i++;
echo '<fieldset id="group">';
echo '<label for="' . $row['answer1'] . '"><input type="radio" id="' . $row['answer1'] . '"name="answers' . $i . '" value="' . $row['answer1'] . '"> <bled></bled>
<span>' . $row['answer1'] . '</span></label>' . '<br>';
echo '<label for="' . $row['answer2'] . '"><input type="radio" id="' . $row['answer2'] . '"name="answers' . $i . '" value="' . $row['answer2'] . '"> <bled></bled>
<span>' . $row['answer2'] . '</span></label>' . '<br>';
echo '<label for="' . $row['answer3'] . '"><input type="radio" id="' . $row['answer3'] . '"name="answers' . $i . '" value="' . $row['answer3'] . '"> <bled></bled>
<span>' . $row['answer3'] . '</span></label>' . '<br>';
echo '<label for="' . $row['answer4'] . '"><input type="radio" id="' . $row['answer4'] . '"name="answers' . $i . '" value="' . $row['answer4'] . '"> <bled></bled>
<span>' . $row['answer4'] . '</span></label>' . '<br>';
echo '</fieldset>';
echo '</div></div>';
}
?>
<input type="submit" value="Pateikti atsakymus" name="result" class="qSubmit" id="qSubmit" />
</form>
您需要綁定提交事件處理程序,而不是理想情況下單擊提交按鈕,並防止表單的默認行為。
$(document).ready(function() { $("#quiz").on('submit', function(e) { var totalRadio = $(":radio").length; if ($('radio:checked').length !== totalRadio) { alert("not all checked"); e.preventDefault(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="bbb.php" method="post" id="quiz" style="margin-top:100px;"> Check 1<input type="radio" name="input1"> Check 1<input type="radio" name="input2"> Check 1<input type="radio" name="input3"> Check 1<input type="radio" name="input4"> Check 1<input type="radio" name="input5"> Check 1<input type="submit" value="Pateikti atsakymus" name="result" class="qSubmit" id="qSubmit" /> </form>
您需要阻止按鈕的默認操作,即提交表單
$("#qSubmit").click(function(event) {
if ($(':radio:checked').length !== count) {
// prevent submit
event.preventDefault();
alert("not all checked");
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.