简体   繁体   中英

to get selected value in select tag

<div>
<select name="mySelect" id="mySelect" class="select"  onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value="english.php">English</option>
<option value="french.php">French</option>
<option value="spanish.php">Spanish</option>
</select>
</div>

i want to change language it work fine when i change the language but in dropdown it always shows english

在页面加载时,将selected属性添加到要选择的选项。

You need to add another tag that is blank.

Working example: http://jsfiddle.net/XYSXA/

<select name="mySelect" id="mySelect" class="select"  onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
    <option value=""></option>
    <option value="english.php">English</option>
    <option value="french.php">French</option>
    <option value="spanish.php">Spanish</option>
</select>​

This way none of the options will be selected by default.

Alternatively, you may use the set the attribute selected inside one of the opening <option> tags to specify which language should be selected by default. Example selecting French by default:

<select name="mySelect" id="mySelect" class="select"  onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
    <option value=""></option>
    <option value="english.php">English</option>
    <option value="french.php" selected>French</option>
    <option value="spanish.php">Spanish</option>
</select>​

Working example: http://jsfiddle.net/XYSXA/1/

<?
$file=basename($_SERVER['PHP_SELF'],".php");
$file=ucwords($file);
?>
<option value="french.php" <?=($file=="French")?" selected='selected'":""?>>French</option>

I m assuming that you're using the pattern like French for french.php, Spanish For spanish.php,etc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM