0

我想制作一个简单的验证表单,它会在取消选中时显示警告消息<div>

这是什么,我到现在为止。

<form action="result.php" method="post">
<b>1st question:</b><br />
Option 1 <input type="radio" name="question1" value="Option1" /><br />
Option 2 <input type="radio" name="question1" value="Option2" /><br />
Option 3 <input type="radio" name="question1" value="Option3" /><br />
Option 4 <input type="radio" name="question1" value="Option4" /><br />
<br />
<b>2nd question:</b><br />
Option 1 <input type="radio" name="question2" value="Option1" /><br />
Option 2 <input type="radio" name="question2" value="Option2" /><br />
Option 3 <input type="radio" name="question2" value="Option3" /><br />
Option 4 <input type="radio" name="question2" value="Option4" /><br />
<br />
<input type="submit" value="submit" />
</form>`
4

1 回答 1

0

你可以通过 jQuery 来做到这一点。length它计算的属性 jQuery 对象中的元素数量

$("#forms").submit(function(){ //or you can use click event 
   if ($("input[name='question1']:checked").length > 0){
  $('.show_message').html(' ');
  $('.show_message').html('selected  ');
}
else{
   $('.show_message').html(' ');
   $('.show_message').html(' no  one selected ');
   return false;
}

});

<div class="show_message"></div>
<form id="form" action="result.php" method="post">
 . . . .
</form>

jsFiddle

于 2012-12-13T09:16:30.630 回答