php html 选择选项 SELECTED

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12782373/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:13:08  来源:igfitidea点击:

html select option SELECTED

phphtmloptionselected

提问by Jam Ville

I have in my php

我在我的 php

$sel = "
    <option> one </option>
    <option> two </option>
    <option> thre </option>
    <option> four </option>
";

let say I have an inline URL = site.php?sel=one

假设我有一个内联 URL = site.php?sel=one

if I didn't saved those options in a variable, I can do it this way to make one of the option be SELECTED where value is equal to $_GET[sel]

如果我没有将这些选项保存在变量中,我可以通过这种方式使其中一个选项成为 SELECTED 其中 value 等于 $_GET[sel]

<option <?php if($_GET[sel] == 'one') echo"selected"; ?> > one </option>
<option <?php if($_GET[sel] == 'two') echo"selected"; ?> > two </option>
<option <?php if($_GET[sel] == 'three') echo"selected"; ?> > three </option>
<option <?php if($_GET[sel] == 'four') echo"selected"; ?> > four </option>

but the problem is, I need to save those options in a variable because I have a lot of options and I need to call that variable many times.

但问题是,我需要将这些选项保存在一个变量中,因为我有很多选项,我需要多次调用该变量。

Is there a way to make that option be selected where value = $_GET[sel]?

有没有办法让那个选项被选中value = $_GET[sel]

回答by Sirko

Just use the array of options, to see, which option is currently selected.

只需使用选项数组即可查看当前选择了哪个选项。

$options = array( 'one', 'two', 'three' );

$output = '';
for( $i=0; $i<count($options); $i++ ) {
  $output .= '<option ' 
             . ( $_GET['sel'] == $options[$i] ? 'selected="selected"' : '' ) . '>' 
             . $options[$i] 
             . '</option>';
}

Sidenote: I would define a value to be some kind of id for each element, else you may run into problems, when two options have the same string representation.

旁注:我会为每个元素定义一个值作为某种 id,否则当两个选项具有相同的字符串表示时,您可能会遇到问题。

回答by Mohit Mehta

foreach($array as $value=>$name)
{
    if($value == $_GET['sel'])
    {
         echo "<option selected='selected' value='".$value."'>".$name."</option>";
    }
    else
    {
         echo "<option value='".$value."'>".$name."</option>";
    }
}

回答by Muhammad Fahad

This is simple example by using ternary operator to set selected=selected

这是使用三元运算符设置 selected=selected 的简单示例

<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $key => $value) { ?>
  <option value="<?php echo $key;?>" <?php echo ($key ==  '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>

回答by TRiG

foreach ($array as $value => $name) {
     echo '<option value="' . htmlentities($value) . '"' . (($_GET['sel'] === $value) ? ' selected="selected"') . '>' . htmlentities($name) . '</option>';
}

This is fairly neat, and, I think, self-explanatory.

这相当简洁,而且我认为不言自明。

回答by Rob

You're missing quotes for $_GET['sel']- fixing this might help solving your issue sooner :)

您缺少引号$_GET['sel']- 修复此问题可能有助于更快地解决您的问题:)