示例javascript:
<script type="text/javascript">
function validateForm() {
var radiobutton = document.getElementsByName("co[]");
for (var i = 0; i < radiobutton.length; i++) {
if (radiobutton[i].checked) {
return true;
}
}
alert("At least one radio button has not been selected");
return false;
}
示例php:
<form method="post" action="result.php" onSubmit="return validateForm();">
<?php
$_SESSION['two']=$_POST;
$query1 = mysql_query('SELECT * FROM option WHERE code IN("'.implode('","',$_POST['co']).'")');
while($data1 = mysql_fetch_assoc($query1))
{
echo $data1['name'];
?>
<table>
<tr>
<td>Detail</td>
<td>Select</td>
</tr>
<?php
$query = mysql_query('SELECT * FROM detailoption WHERE code="'.$data1['code'].'"');
while($data = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$data['detail']."</td>";
echo "<td><input type='radio' value='".$data['detail']."' name='co[".$data['code']."]' /></td>";
echo "</tr>";
}
?>
</table>
<?php
}
?>
<input type="submit" name="submit" value="Submit" />
当我选择单选按钮并提交时,系统会显示错误消息 当我没有选择单选按钮并提交时,系统也会显示错误消息。
任何人都可以帮助我吗?
答案 0 :(得分:0)
您的行var radiobutton = document.getElementsByName("co[]");
始终返回空数组,因为您没有任何具有该名称的输入。
您应该将code
信息存储在输入的value
属性中,如果要检索结果中的detail
字段,则应存储detailoption_id
在value
属性中,因为表中的所有行都具有相同的代码,但可能不是相同的细节。
$query = mysql_query('SELECT * FROM detailoption WHERE code="'.$data1['code'].'"');
while($data = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$data['detail']."</td>";
echo "<td><input type='radio' value='".$data['code']."' name='co' /></td>";
echo "</tr>";
}
然后从新名称
获取输入列表 var radiobutton = document.getElementsByName("co");
答案 1 :(得分:0)
首先为表单形式提供一个名称,然后尝试这个。我希望它能用于你。
function validateForm() {
len = document.myform.co.length
for (i = 0; i <len; i++)
{
if (document.myform.co[i].checked)
{
return true;
}
alert("At least one radio button has not been selected");
return false;
}