0

我正在通过 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>";
4

4 回答 4

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>";
于 2013-10-28T06:17:29.543 回答
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>';
于 2013-10-28T06:16:47.970 回答
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>';
于 2013-10-28T20:24:25.237 回答
0

我们不需要double quotes类型。试试

echo "<td><input type='radio' name='".$row['voter_id']. "' value='1' "></td>";
于 2013-10-28T06:17:36.300 回答