Php 动态填充复选框
简单的问题, 我试图动态地填充html表单复选框,但当我试图通过在html中使用复选框的checked属性来实现这一点时,它无法正常工作。无论我是否使用Php 动态填充复选框,php,javascript,html,Php,Javascript,Html,简单的问题, 我试图动态地填充html表单复选框,但当我试图通过在html中使用复选框的checked属性来实现这一点时,它无法正常工作。无论我是否使用checked='0'/checked='1',checked='true'/checked='false'和checked='checked'/checked='unchecked',复选框都保持选中状态 虽然我通过在php中应用额外的检查解决了这个问题,如下所示 while($contact = $db->fetchByAssoc($c
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>
while($contact=$db->fetchByAssoc($contacts))
{
$include_checked=($contact['include'])?“checked={$contact['include']}”:”;
$opted_checked=($contact['opted_out'])?“checked={$contact['include']}”:”;
$html.=“{$contact['name']}
有更好的解决方案吗?选中的
属性的值无关紧要。如果要取消选中选中的属性,请不要设置它
<input type="checkbox" />
使用是
预先选择它。默认情况下,删除它取消选中它
<input type="checkbox" checked="yes"/>
或`checked=“checked”
或,但不推荐使用,不鼓励使用:
<input type="checkbox" checked />
未选中?根本不使用任何选项。只需删除它。通过后端,没有办法不选择它。当然,这一点也不需要。是的,我是通过php代码中的检查来完成的,但是为什么HTML对选中的属性只有一个受限选项?还有其他选项吗?这是个问题。@mansoor我不知道有任何受限选项选中属性值(可能在XHTML中?)。如果有带有任何值的选中的属性,则将选中它,如果没有选中的属性,则将取消选中它。
<input type="checkbox" checked />