php 表单上单选按钮的 javascript 验证问题 [英] Issues with javascript validation of radio buttons on php form
问题描述
这是包含带有单选按钮的表单的 php 文件的代码:
This is the code for the php file containing the form with radio buttons:
<?php
while($r7 = mysql_fetch_array($rt3)){
$name=$r7["name"];
$s=$r7["question"];
echo "
<div>$s</div>
<form name='Tester' method='post' onSubmit='return RegValidate(this);' action='quest.php' >
<input type='radio' name='w1' value='1'>1<br>
<input type='radio' name='w1' value='2'>2<br>
<input type='radio' name='w1' value='3'>3<br>
";
}
echo" <input name='Submit' value='Tester' type='submit' />
</form>";
?>
$name 和 $q 来自使用 mysql 查询的数据库.这工作正常.到目前为止,我已经使用了以下 javascript 代码:
The $name and $q are derived from the database using a mysql query. This works fine. I have used the following javascript code so far:
function RegValidate(Tester)
{
if($('#w1').not(':checked'))
{
alert('Radio not checked');
return false;
}
}
但是使用此代码,我继续收到错误消息,即使我选择了一个单选按钮,我也无法继续.
But with this code, I continue to get the error message, and I am not able to move on even though I have selected a radio button.
while 循环生成多组单选按钮,如 $q,从数据库中获取问题并将其与 3 个单选按钮一起发布到页面上.每个问题 $q,上面都有 3 个单选按钮,这就是 $name 的原因.
The while loop produces several sets of radio buttons, as $q, gets the question from the database and posts it on the page along with 3 radio buttons. Each question $q, has the 3 radio buttons in the above, hence the reason for the $name.
如何使用 javascript 验证此表单.注意.该表单将仅包含单选按钮.
How do I validate this form with javascript. NB. The form will only contain radio buttons.
推荐答案
function RegValidate(Tester) {
if ($(":radio[name=w1]:checked").toArray().length == 0) {
alert('nothing selected');
return false;
}
}
这篇关于php 表单上单选按钮的 javascript 验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!