php 如何在 HTML 的下拉列表中设置选定的值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14647817/
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 07:41:48  来源:igfitidea点击:

How to set selected value in drop list in HTML?

phphtmlmysql

提问by NamshanNet

How to set selected value in drop list in HTML in Edit Form?

如何在编辑表单的 HTML 下拉列表中设置选定值?

The selected value came from MySql

所选值来自 MySql

This code is not work !! >> selected=""

这段代码不起作用!!>> 选择=""

<select name="TutorGender" id="TutorGender" selected="<?php echo $Gender ?>">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
</select>

回答by cepatt

W3Schools does a good job of explaining this here http://www.w3schools.com/tags/att_option_selected.asp

W3Schools 在这里很好地解释了这一点 http://www.w3schools.com/tags/att_option_selected.asp

Your code might look something like this.

您的代码可能如下所示。

 <select name="TutorGender" id="TutorGender">
      <option value="Male" <?php if($Gender == "Male") echo "selected"; ?>>Male</option>
      <option value="Female" <?php if($Gender == "Female") echo "selected"; ?>>Female</option>
 </select>

Though my PHP syntax may be a little off, but you should get the idea.

虽然我的 PHP 语法可能有点偏差,但您应该明白这一点。

回答by Gareth Cornish

<select name="TutorGender" id="TutorGender">
    <option value="Male" <?php if($Gender=='Male') echo 'selected' ?>>Male</option>
    <option value="Female" <?php if($Gender=='Female') echo 'selected' ?>>Female</option>
</select>

回答by Zevi Sternlicht

Selected goes on the optiontag, not on the selecttag.

Selected 在option标签上,而不是在select标签上。

<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>

回答by Quentin

The selectedattribute appears on the option(s) that should be selected, not the select element (if it went on the select element itself, then (since multiple options can be selected at the same time) it would have to take a list of ids and you would need to add an id to each option).

selected属性出现在应该选择的选项上,而不是选择元素上(如果它出现在选择元素本身上,那么(因为可以同时选择多个选项)它必须采用ids的列表并且您需要为每个选项添加一个 id)。

<option value="foo" selected>Foo</option>

Generally you would have an array of values (or of value/human text pairs) that you would loop over. (i.e. you wouldn't hard code your options)

通常,您将拥有一个循环遍历的值(或值/人类文本对)数组。(即您不会硬编码您的选项)

You would generate one option each time you went around the loop, and test to see if you should include a selected attribute each time.

每次循环时,您都会生成一个选项,并测试是否每次都应包含选定的属性。

回答by Wezy

<option <?php if($Gender == 'Male'){ echo(' selected '); } ?> value="Male">Male</option>
<option <?php if($Gender == 'Female'){ echo(' selected '); } ?> value="Female">Female</option>

回答by rlatief

<select name="TutorGender" id="TutorGender">

<?php
$genders = array( 'Male', 'Female' );
foreach( $genders as $value )
{
    $selected = ( $Gender == $value )? ' selected="selected"': '';
    echo '<option value="' . $value . '"' . $selected . '>' . $value . '</option>';
}
?>

</select>

回答by thatNewGuy

just another way of doing it. I prefer to use shorthand if statements for code such as this.

只是另一种方式。对于诸如此类的代码,我更喜欢使用速记 if 语句。

<select name="TutorGender" id=\"TutorGender\">
  <option value="Male" <?php echo ($Gender == "Male" ? "selected" : ""); ?>>Male</option>
  <option value="Female" <?php echo ($Gender == "Female" ? "selected" : ""); ?>>Female</option>

here is a linkto using shorthand "if" statements.

这是使用速记“if”语句的链接

回答by Wil DeCarde

In a case where I needed to use a select as part of a user update form that was populated via a database I found the array option works best. The line by line option can get very unwieldy very quickly. This way you simply need to add an option to the $optionDept array should any new options be required. In this example "$row['Dept']" is from the larger overarching results from the database request.

在我需要使用选择作为通过数据库填充的用户更新表单的一部分的情况下,我发现数组选项效果最好。逐行选项很快就会变得非常笨拙。这样,如果需要任何新选项,您只需向 $optionDept 数组添加一个选项。在此示例中,“$row['Dept']”来自数据库请求的较大总体结果。

$optionDept= array('sales','warehouse','inspection','production','accounts','admin','inactive');

$optionCount= count($optionDept);

echo"<select name=\"UserDept\">";
for($x = 0; $x < $optionCount; $x++) {
echo"<option value=".$optionDept[$x];
if($optionDept[$x]==$row['Dept']){echo " selected";}
echo">".$optionDept[$x]."</option>";}
echo"</select</P>

回答by user3318789

This worked for me: I send througt the GET method of the submit the value i want to restore, and then in the php, AT THE END OF THE PAGE (if not, default values will be set if the code is before the php) i set those values.

这对我有用:我通过 GET 方法发送提交我想要恢复的值,然后在 php 中,在页面的末尾(如果没有,如果代码在 php 之前,则将设置默认值)我设置了这些值。

echo "select_language.selectedIndex = ".$_GET['iL'].";";

回答by Swapnil T

try this

尝试这个

if you want to keep male as selected

如果你想保持男性被选中

<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>