我正在通过PHP动态生成单选按钮,但它无法正常工作。我无法取消选中一个单选按钮,一旦我选择了另一个具有相同名称的单选按钮,则不会自动取消选中。
echo "<form action="."".">";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td>" . $row['voter_id'] . "</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type="."radio"." name=".$row['voter_id']. "value="."1"."></td>";
echo "<td><input type="."radio"." name=".$row['voter_id'] ."value="."2"."></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
答案 0 :(得分:2)
尝试以下
echo "<form action=''>";
echo "<table border='1'>
<tr>
<th>Reg Num</th>
<th>Select</th>
<th>Reject</th>
</tr>";
/* <th>Name</th>
<th>State</th>
<th>Constituency</th> */
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
// echo "<td> $row['voter_id']</td>";
// echo "<td>" . $row['name'] . "</td>";
// echo "<td>" . $row['state'] . "</td>";
// echo "<td>" . $row['constituency'] . "</td>";
echo "<td>" . $row['numreg'] . "</td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "<td><input type='radio' name='voter_id' value='$row[voter_id]'></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
答案 1 :(得分:1)
为此,单选按钮的名称必须相同。在创建单选按钮的while循环内更改代码,如下所示
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="voter_id" id="'.$row["voter_id"] .'"value="2"></td>';
答案 2 :(得分:0)
我们不需要double quotes
类型。请尝试
echo "<td><input type='radio' name='".$row['voter_id']. "' value='1' "></td>";
答案 3 :(得分:0)
最后我做了这个并且有效了
echo '<td><input type="radio" name="'.$row["voter_id"]. '"value="1"></td>';
echo '<td><input type="radio" name="'.$row["voter_id"] .'"value="2"></td>';