0

所以,我有这个价值:

$row['status'];

如何将已检查的值标记为已检查?这是表格:

<input type="radio"  id="status" name="status" value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" value="0" /> Hapur<br />

因此,如果值为 1,则应选中单选按钮.. 谢谢

4

6 回答 6

1

您应该首先检查变量是否已设置,否则您将得到Undefined variable: row-Error 然后检查它的值:

<input type="radio"  id="status" name="status" <?php if(isset($row['status']) && $row['status'] == 1){ ?> checked="checked" <?php } ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status2" name="status2" <?php if(isset($row['status2']) && $row['status2'] == 0){ ?> checked="checked" <?php } ?> value="0" /> Hapur<br />

编辑:您不应在页面上多次使用 ID(“状态”)。ID 是唯一标识符。

于 2012-12-18T12:20:58.290 回答
1
<input type="radio"  id="status" name="status" <?php if($row['status'] == 1){ ?> checked="checked" <?php } ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" <?php if($row['status'] == 0){ ?> checked="checked" <?php } ?> value="0" /> Hapur<br />
于 2012-12-18T12:18:45.097 回答
0

Chris answer is absolutely correct except both radio buttons having same id.
With all due respect to those who has answered, it is just my suggestion that no two elements in page should have same id.

于 2012-12-18T12:33:06.647 回答
0
<input type="radio" id="status" name="status" value="1" <?=($row[ 'status' ] ? "checked" : "") ?>/>
<input type="radio" id="status" name="status" value="0" <?=($row[ 'status' ] ? "" : "checked") ?>/>
于 2012-12-18T12:17:50.233 回答
0

这应该有效:

<input type="radio"  id="status" name="status" <?=($row['status'] == 1 ?"checked=\"checked\"" : "") ?> value="1" /> Mbyllur<br />
<input type="radio"  id="status" name="status" <?=($row['status'] == 0 ?"checked=\"checked\"" : "") ?> value="0" /> Hapur<br />
于 2012-12-18T12:18:20.730 回答
0
if ($row['status'] == 1) { echo 'checked="checked"'; }
于 2012-12-18T12:18:23.273 回答