我想知道如何正确选中复选框。
当前在内部循环中循环...
在第一个foreach
中,它列出了所有要标记的选项,如下图所示:
在第二个foreach
中,将标记来自数据库的选项。它使用此代码<?php if($checkIni[$key] == "tem"){echo "checked ";}; ?>
进行检查。更改的是 TEM,NAO,AVA 的值。
<?php
$checklistIni = "a:16:{i:0;s:3:"nao";i:1;s:3:"ava";i:2;s:3:"tem";i:3;s:3:"tem";i:4;s:3:"tem";i:5;s:3:"tem";i:6;s:3:"tem";i:7;s:3:"tem";i:8;s:3:"tem";i:9;s:3:"tem";i:10;s:3:"tem";i:11;s:3:"tem";i:12;s:3:"tem";i:13;s:3:"tem";i:14;s:3:"tem";i:15;s:3:"tem";}";
$checkIni = unserialize($checklistIni );
foreach($rsChecklist as $checklist) {
foreach($checkIni as $key => $val){
?>
<tr>
<td class="texto-branco">
<?php echo $checklist['nome'] ?>
</td>
<td><label><input value="tem" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "tem"){echo "checked ";}; ?> /><span></span></label></td>
<td><label><input value="nao" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "nao"){echo "checked ";}; ?> /><span></span></label></td>
<td><label><input value="ava" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "ava"){echo "checked ";}; ?> /><span></span></label></td>
</tr>
<?php
};
};
?>
答案 0 :(得分:1)
您的代码有些混乱,希望我能为您提供帮助:
$checklistIni = 'a:16:{i:0;s:3:"nao";i:1;s:3:"ava";i:2;s:3:"tem";i:3;s:3:"tem";i:4;s:3:"tem";i:5;s:3:"tem";i:6;s:3:"tem";i:7;s:3:"tem";i:8;s:3:"tem";i:9;s:3:"tem";i:10;s:3:"tem";i:11;s:3:"tem";i:12;s:3:"tem";i:13;s:3:"tem";i:14;s:3:"tem";i:15;s:3:"tem";}';
$checkIni = unserialize($checklistIni);
$rsChecklist = array(
array(
'nome' => 'A',
)
);
foreach ($rsChecklist as $k => $checklist) {
foreach ($checkIni as $key => $val) {
?>
<table>
<tr>
<td class="texto-branco">
<?php echo $checklist['nome'] ?>
</td>
<td><label><input value="tem" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "tem") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
<td><label><input value="nao" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "nao") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
<td><label><input value="ava" name="checklistFim<?php echo $key ?>" class="checklistFim"
type="radio" <?php if ($val == "ava") {
echo "checked='checked' ";
}; ?> /><span></span></label></td>
</tr>
</table>
<?php
}
}
基本上,您必须更改这些行<input value="tem" name="checklistFim<?php echo $checklist['ID_Checklist'] ?>" class="checklistFim" type="radio" <?php if($checkIni[$key] == "tem"){echo "checked ";}; ?> />
name
属性对于通道中的三个无线电中的每一个必须相同,并且与其他通道不同。