0

ドロップダウン リストの最初の項目を空白にしてデフォルトで選択することはできますが、ドロップダウン リストで項目を選択して送信を押すと、その値がそこにとどまります。

ここに例があります

<form action="" method="post" />
<SELECT NAME="whatever" id="whatever">
<?php $selected = isset($_POST["whatever"]) && $_POST["whatever"] == 'whatever'; ?>
<option value="" hidden="true" selected="selected"></option>
<OPTION VALUE=1 onclick"selected='selected'">All</OPTION>
</SELECT> 
<input type="submit" name="submit" value="Update"/>
</form>

現在、送信を押すと、選択した値が空白に戻ります。

4

2 に答える 2

0
function html_select($name, $options, $selected= "")
{
    // open <select> tag, set id & name attributes
    $select = "<select id=\"$name\" name=\"$name\">\n";

    // populate options to drop down list
    foreach ($options as $value=> $html) {
        $select .= "<option value=\"$value\"";

        // select the selected option
        if ($value == $selected) {
            $select .= " selected=\"selected\"";
        }

        $select .= ">$html</option>\n";
    }

    // close </select> tag
    $select .= "</select>";

    // return element
    return $select ;
}

echo html_select("whatever", array("" => "Select Value", "1" => "Value 1"), $_POST["whatever"]);

これが助けになることを願っています。

于 2013-05-21T09:27:12.273 に答える
0

あなたはこのように使うかもしれません

<form action="" method="post" />
<SELECT NAME="whatever" id="whatever">
<option value="">Select value</option>
<OPTION VALUE=1 <?php if($_POST["whatever"]==1) echo "selected='selected'";?> >All</OPTION>
<OPTION VALUE=2 <?php if($_POST["whatever"]==2) echo "selected='selected'";?> >Value One</OPTION>
</SELECT> 
<input type="submit" name="submit" value="Update"/>
</form>
于 2013-05-21T09:14:49.150 に答える