是否可以重复具有不同检查值的相同单选按钮?

时间:2012-07-26 10:59:46

标签: php html dynamic radio

我的问题是关于单选按钮。我有10个具有相同名称和顺序值的单选按钮。检查其中一个单选按钮。示例代码如下:

<li><input type="radio" value="1000" checked="checked" name="status" />Yet to contact</li>
<li><input type="radio" value="1001" name="status"/>To call back, follow up</li>
<li><input type="radio" value="1002" name="status"/>Interested, to meet</li>
<li><input type="radio" value="1003" name="status"/>Meeting over, to follow up</li>
<li><input type="radio" value="1004" name="status"/>Meeting over, not interested</li>
<li><input type="radio" value="1005" name="status"/>Not interested now</li>
<li><input type="radio" value="1006" name="status"/>Wrong contact details</li>
<li><input type="radio" value="1007" name="status"/>Services taken</li>

上面的代码将是一个通过PHP的动态代码,并将在单页上重复多次。但是,我尝试使用普通的html代码,发现它会产生问题并且不会显示任何单选按钮。如果我使用不同的名称对其进行分组,例如name =“status [1]”和name =“status [2]”,则只有每组单选按钮都会显示一个选中的单选按钮。

有没有人有一个解决方案,我可以为所有单选按钮保持相同的名称,每个组(同名)的单选按钮会显示我选择的默认单选按钮?

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题:

在单选按钮的name属性中指定一个数组。

<input  type="radio" name="check[]" value="1" />
<input  type="radio" name="check[]" value="2" />
<input  type="radio" name="check[]" value="3" />

使用$_POST['check']时,您将获得一个包含索引和单选按钮值的数组。

假如你检查了第一个和第三个单选按钮,那么数组将是:

array('0'=>'1', '2'=>'3')

答案 1 :(得分:0)

每组radiobuttons的名称都是唯一的。

尝试这样的事情:

for ($i=1; $i<=5; $i++) {
   echo '<li><input type="radio" value="1000" checked="checked" name="status'.$i.'" />Yet to contact</li>
         <li><input type="radio" value="1001" name="status'.$i.'"/>To call back, follow up</li>

         <li><input type="radio" value="1002" name="status'.$i.'"/>Interested, to meet</li>

         <li><input type="radio" value="1003" name="status'.$i.'"/>Meeting over, to follow up</li>

         <li><input type="radio" value="1004" name="status'.$i.'"/>Meeting over, not interested</li>

         <li><input type="radio" value="1005" name="status'.$i.'"/>Not interested now</li>

         <li><input type="radio" value="1006" name="status'.$i.'"/>Wrong contact details</li>

         <li><input type="radio" value="1007" name="status'.$i.'"/>Services taken</li>';
}

要阅读所选值,请使用:

for ($i=1; $i<=5; $i++) {
   echo $_POST["status$i"] . '<br>;
}
相关问题