0

这是另一个。同一天两次。我正在尝试设置 4 个选择框,如果它们等于 enSlot 的值,则显示基于 Target1-4 中的值选择的选项。

echo "<select id='target".$row['paSlot']."' size='1' style='width:90px;'>";
$query = "SELECT * FROM enemy WHERE enChar = ".$_SESSION['chNum']." ORDER BY enSlot";
$result1 = mysql_query($query, $_SESSION['connect']) or die('Error 150: '.mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
    $s = "";
    if ($_GET['Target1'] == $row1['enSlot']) {
            $s = "selected='selected'";
    }
    if ($_GET['Target2'] == $row1['enSlot']) {
            $s = "selected='selected'";
    }
    if ($_GET['Target3'] == $row1['enSlot']) {
            $s = "selected='selected'";
    }
    if ($_GET['Target4'] == $row1['enSlot']) {
            $s = "selected='selected'";
    }
    echo "<option value=".$row1['enSlot']." ".$s.">".$row1['enSlot'].". ".$row1['enRace'];
}
echo "</select>";

我用这个运行它:

Target1 = 1, Target2 = 2, Target3 = 3, Target4 = 4 敌人有 4 条记录,enSlot = 1 - 4

当它执行时,我得到所有 4 个选择框显示 Target4 的最后一个值被选中。

4

2 回答 2

2
echo "<option value=".$row1['enSlot']." ".$s.">".$row1['enSlot'].". ".$row1['enRace'];

我看到不见了</option>

于 2012-04-12T14:37:29.683 回答
1

问题出在您的逻辑中,无论哪种条件评估 $s 的值总是会被​​选中=选中...您可以试试这个:

while ($row1 = mysql_fetch_array($result1)) {
    foreach( $_GET[] as $target ) {
        if( $target == $row1['enSlot'] ) {
            echo "<option value=".$row1['enSlot']." selected=selected>".$row1['enSlot'].". ".$row1['enRace']."</option>";
        } else {
            echo "<option value=".$row1['enSlot'].">".$row1['enSlot'].". ".$row1['enRace']."</option>";

        }
    }
}
于 2012-04-12T15:06:36.970 回答