动态填充复选框

时间:2012-08-27 12:50:13

标签: php javascript html

简单的问题, 我试图动态填充html表单复选框,但是当我尝试通过使用html中的复选框的checked属性来执行此操作时,它无法正常工作。选中此复选框是否使用checked='0' / checked='1'checked='true' / checked='false'checked='checked' / checked='unchecked'

虽然我通过在php中应用额外的检查来解决它...

while($contact = $db->fetchByAssoc($contacts))
{
$include_checked =( $contact['include'])? " checked={$contact['include']} ": "";
$opted_checked = ($contact['opted_out'])? " checked={$contact['include']} ": "";

$html.="<tr><td>{$contact['name']} </td>
            <td><input type='checkbox' {$include_checked}/> </td>    
            <td><input type='checkbox' {$opted_checked}/> </td>

有没有更好的解决方案呢???

2 个答案:

答案 0 :(得分:0)

checked属性的值无关紧要。如果您不想要它,请不要设置checked属性。

<input type="checkbox" />

答案 1 :(得分:0)

使用yes预选。删除它以默认取消选中

<input type="checkbox" checked="yes"/>

或`checked =“选中”

<input type="checkbox" checked="checked"/>

或者,但已弃用且不鼓励使用:

<input type="checkbox" checked />

未检查?根本不要使用任何选项。只需删除它。通过后端,没有办法不选择它。当然,这根本不需要。