我有一个循环回应了一些元素。我也有一个if语句,应用复选框'已检查'如果复选框元素包含数据库中的某个字符串,则属性为复选框元素,例如:
if ($team == $row['team1']) {
$checked1 = "checked='checked'";
}
else {
$checked1 = "";
}
if ($team == $row['team2']) {
$checked2 = "checked='checked'";
}
else {
$checked2 = "";
}
echo "<div><input type='radio' name='games" . $i . "' value='" . $row['team1'] . "' " . $checked1 . "></div>";
echo "<div><input type='radio' name='games" . $i . "' value='" . $row['team2'] . "' " . $checked2 . "></div>";
这似乎工作正常。但我也想在复选框周围的div中添加一个类,例如:
if ($team == $row['team1']) {
$checked1 = "checked='checked'";
$div1 = "class='green'";
}
else {
$checked1 = "";
$div1 = "";
}
if ($team == $row['team2']) {
$checked2 = "checked='checked'";
$div2 = "class='green'";
}
else {
$checked2 = "";
$div2 = "";
}
echo "<div " . $div1 . "><input type='radio' name='games" . $i . "' value='" . $row['team1'] . "' " . $checked1 . "></div>";
echo "<div " . $div2 . "><input type='radio' name='games" . $i . "' value='" . $row['team2'] . "' " . $checked2 . "></div>";
事情就是这个类似乎将自己应用于由循环产生的所有divs unscriminatly。这是否只是作为循环和回显元素的副产品出现的?有没有更好的方法来实现这一点(可能使用JQuery)。
编辑:添加了HTML结果
<div class='green'><input type='radio' name='games1' value='myteam' checked='checked'></div>"
<div><input type='radio' name='games1' value='yourteam' ></div>
<div class='green'><input type='radio' name='games1' value='myteam' ></div>"
<div><input type='radio' name='games1' value='yourteam' checked='checked'></div>
<div class='green'><input type='radio' name='games1' value='myteam' ></div>"
<div><input type='radio' name='games1' value='yourteam' checked='checked'></div>
<div class='green'><input type='radio' name='games1' value='myteam' checked='checked'></div>"
<div><input type='radio' name='games1' value='yourteam' ></div>"
答案 0 :(得分:1)
我怀疑问题出在你的循环中,但我会尝试在你的逻辑之前声明变量。
这将确保每次都清除它们:
$checked1 = "";
$div1 = "";
$checked2 = "";
$div2 = "";
if ($team == $row['team1']) {
$checked1 = ' checked="checked"';
$div1 = ' class="green"';
}
if ($team == $row['team2']) {
$checked2 = ' checked="checked"';
$div2 = ' class="green"';
}
echo '<div' . $div1 . '><input type="radio" name="games' . $i . '" value="' . $row['team1'] . '"' . $checked1 . '></div>';
echo '<div' . $div2 . '><input type="radio" name="games' . $i . '" value="' . $row['team2'] . '"' . $checked2 . '></div>';
此外,如果$team
为空,且$row[team1]
为空,则评估为true。
答案 1 :(得分:0)
(我会发表评论,但我还没有足够的声誉。)
无论如何,你有这个理由:
$div1 ="class='green'";
$div2 ="class='green'";
?当两个变量具有相同的值时,所有类看起来都是一样的。