开发者

Specify a default selected item for HTML form drop-downs

开发者 https://www.devze.com 2023-01-14 12:20 出处:网络
Typically when you need to select an item by default, you do: <select> <option value=\"1\">Volvo</option>

Typically when you need to select an item by default, you do:

<select>
  <option value="1">                 Volvo  </option>
  <option value="2" selected="true"> Saab  </option>
  <option value="3">                 Mercedes  </option>
  <option value="4">                 Audi  </option>
</select>

Is it possible to get something like this?

<select selectedValue="2">
  <option value="1">  Volvo  </option>
  <option value="2">  Saab  </option>
  <option value="3">  Mercedes  </option>
  <option value="4">  Audi  </option>
</select>

It works out easier in PHP since you only have to sof开发者_运维问答t-code one value, instead of handling the selected attribute on any possible <option/>.


There is no attribute like that on the <select> element. Assuming your <option> output is in a loop, I don't see how it makes a huge difference:

$selected = "2";
foreach($values as $key => $val) {
    echo "<option value=\"" . $key . "\"" . ($key == $selected ? " selected=\"selected\">" : ">") . $val . "</option>";
}

(my PHP is a little rusty, that may not be 100% correct)


No, but you can add your default value to your id like

<select id="default-value-2">

then in your options, you have

<option value="2" <?php echo is_this_the_default_value ? selected='true' : '' ?>

or something to that effect(forgive me i forgot my php syntax, but i hope you get the point).

Anyway, that is a dirty fix also, so i suggest just adding a method to check for the default selected tag and printing it selected="selected" when it is the default. You can just call it once if you loop through your select options


Put JavaScript after the declaration of the listbox and set the selected index there:

<script>
document.getElementById('listBoxId').selectedIndex=<?php echo $INDEX ?>;
</script>

Something like this.


You can do it like this:

$value = 1;

<select>
<?php if($value==1) echo "<option selected='true'> Volvo </option>";
else echo "<option> Volvo </option>"; ?>
<?php if($value==2) echo "<option selected='true'> Saab </option>";
else echo "<option> Saab </option>"; ?>
</select>


PHP:

<select>
    <option value="1" <?php echo ($row['status']==1) ? 'selected' : '' ?>>Permitido</option>
    <option value="2" <?php echo ($row['status']==2) ? 'selected' : '' ?>>Bloqueado</option>
</select>


I found myself googling this to see if there was a better way to do it.

The best and cleanest answer is from @roryf, however if you are not looping through your data I thought it would be a lot cleaner to wrap it up in a function:

function set_selected($desired_value, $new_value)
{
    if($desired_value==$new_value)
    {
        echo ' selected="selected"';
    }
}

Then you would use it like this:

<?php $selected_value = 2; ?>

<select>
    <option value="1"<?php set_selected('1', $selected_value); ?>> Volvo  </option>
    <option value="2"<?php set_selected('2', $selected_value); ?>> Saab  </option>
    <option value="3"<?php set_selected('3', $selected_value); ?>> Mercedes  </option>
    <option value="4"<?php set_selected('4', $selected_value); ?>> Audi  </option>
</select>

This would set Saab as selected :)


Preload $statusid from DB then -> (i have to escape ")

PHP:

    $option_0  = '';
    $option_11 = '';
    $option_12 = '';
    $option_13 = '';
    $option_14 = '';
    $option_15 = '';
    $option_16 = '';
    
    
    if ($statusid ==0  ){
        $option_0 = 'selected=\"\"';
    }elseif($statusid ==11 ){
        $option_11 = 'selected=\"\"';
    }elseif($statusid ==12 ){
        $option_12 = 'selected=\"\"';
    }elseif($statusid ==13 ){
        $option_13 = 'selected=\"\"';
    }elseif($statusid ==14 ){
        $option_14 = 'selected=\"\"';
    }elseif($statusid ==15 ){
        $option_15 = 'selected=\"\"';
    }elseif($statusid ==16 ){
        $option_16 = 'selected=\"\"';
    }

HTML:

    ."<select id=\"id\" name=\"name\" >"
    ."  <option {$option_0} value=\"0\">...TEXT...</option>"
    ."  <option {$option_11} value=\"11\">TEXT</option>"
    ."  <option {$option_12} value=\"12\">TEXT</option>"
    ."  <option {$option_13} value=\"13\">TEXT</option>"
    ."  <option {$option_14} value=\"14\">TEXT</option>"
    ."  <option {$option_15} value=\"15\">TEXT</option>"
    ."  <option {$option_16} value=\"16\">TEXT</option>"
    ."</select><br /><br />"

work Perfect for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号