$policy.status 3次循环的值都为1,但是无法进行 radio只选定最后一个 “开” ,其他两个是未选定的状态,这是为什么?
{{foreach from=$policyInfo item=policy}}
{{if $policy.status eq "1"}}
<th><p>开:<input type="radio" checked="checked" name="single1" value="1" /></p><p>关:<input type="radio" name="single2" value="0" /></p></th>
{{else}}
<th><p>开:<input type="radio" name="single1" value="1" /></p><p>关:<input type="radio" checked="checked" name="single2"value="0" /></p></th>
{{/if}}
{{/foreach}}
{{foreach from=$policyInfo item=policy}}
{{if $policy.status eq "1"}}
<th><p>开:<input type="radio" checked="checked" name="single1" value="1" /></p><p>关:<input type="radio" name="single2" value="0" /></p></th>
{{else}}
<th><p>开:<input type="radio" name="single1" value="1" /></p><p>关:<input type="radio" checked="checked" name="single2"value="0" /></p></th>
{{/if}}
{{/foreach}}
如果你要多选 的话, 用checkbox复选框。
$checked_key = -1;
foreach($policyInfo as $k => $v) { // 判定当前索引是否要选择
if($v['status'] == '1') {
$checked_key = $k;
} // 标志位初始化
$policyInfo[$k]['checked'] = false;}if(isset($policyInfo[$checked_key]) {
$policyInfo[$checked_key]['checked'] = true;
}然后smarty 中 判断{{foreach from=$policyInfo item=policy}}
{{if $policy.checked}}
<th><p>开:<input type="radio" checked="checked" name="single1" value="1" /></p><p>关:<input type="radio" name="single2" value="0" /></p></th>
{{else}}
<th><p>开:<input type="radio" name="single1" value="1" /></p><p>关:<input type="radio" checked="checked" name="single2"value="0" /></p></th>
{{/if}}
{{/foreach}}