对于循环中的复选框,我有以下php echo语句:
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;' />";我想根据查询结果将此条件添加到其中:
<?php if($rowbill['battery'] == "Yes") print "checked"; ?>但是我很难得到正确的连接语法,请帮助我。
发布于 2013-01-06 07:28:52
试试这个:
echo "<input name='battery[$i]'" .($rowbill['battery'] == "Yes" ? "checked='checked'" : ""). " type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;' />";发布于 2013-01-06 07:37:03
为了可读性,我更喜欢在变量之前设置
$checked = $rowbill['battery'] == "Yes"? ' checked="checked"' : '';在此之后:
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;'$checked />";否则,只能在一个
echo "<input name='battery[$i]' type='checkbox' id='battery[$i]' value='Yes' style='margin-top:10px; margin-left:15px;'".($rowbill['battery'] == "Yes"? ' checked="checked"' : '')." />";发布于 2013-01-06 07:31:15
试试这个:
'<input name="'.battery[$i].'" type="checkbox" id="'.battery[$i].'" value="Yes" style="margin-top:10px; margin-left:15px;"'.($rowbill['battery'] == 'Yes' ? ' checked="checked"' : '').'>';请注意,当您需要使用''作为输出的一部分时,使用""分隔符如何使连接变得更容易。
https://stackoverflow.com/questions/14177541
复制相似问题