简体   繁体   English

选择框值以href

[英]Select box value to href

I'm add multilingual functionality to my page and having trouble with a select box. 我向页面添加了多语言功能,但选择框出现了问题。 I want to change the language dynamically displayed on a page to match the chosen language in the select box. 我想更改页面上动态显示的语言,以匹配选择框中选择的语言。

This is my html code: 这是我的html代码:

<?php
include_once 'multilingual_common.php';
?>

<!DOCTYPE>

<html>
<head>
    <title>Test Form</title>
</head>
<body>
    <div id="languages">
        <select onChange="if (this.value) window.location.href=this.value">
            <option value="multilingual_test.html?lang=en">English</option>
            <option value="multilingual_test.html?lang=ko">한국어</option>
        </select>
        <a href="multilingual_test.html?lang=en">English</a>
        <a href="multilingual_test.html?lang=ko">한국어</a>
    </div>
    <ul>
        <li><?php echo $lang['CHARACTER_NAME']; ?> Rose</li>
        <li><?php echo $lang['CHARACTER_LEVEL']; ?> 40</li>
        <li><?php echo $lang['CHARACTER_SEX']; ?> female</li>
        <li><?php echo $lang['CHARACTER_SEX']; ?> none</li>
    </ul>
</body>
</html>

It works when clicking the tag but not the select box. 单击标签而不是选择框时,它可以工作。 The onChange event used in tag doesn't work. 标签中使用的onChange事件不起作用。 How should I fix this? 我该如何解决?

You are using a select list. 您正在使用选择列表。 Getting the value is a tad more complex. 获得价值要复杂得多。 A select list has many options . 选择列表有很多options The options are an array under the this object in the onchange event. 选项是onchange事件中this对象下的数组。

The current selected index is stored in this.selectedIndex 当前选定的索引存储在this.selectedIndex

So to get a value, you need to acces the correct option in the array by the selected value 因此,要获取一个值,您需要根据所选值访问数组中的正确选项

this.options[this.selectedIndex].value

is how you get a value from a select list. 是从选择列表中获取值的方式。

To fix your code you should use instead of 要修复您的代码,您应该使用而不是

<select onChange="if (this.value) window.location.href=this.value">

you need to get the value of the option 您需要获取期权的价值

<select onChange="if (this.options[this.selectedIndex].value) window.location.href=this.options[this.selectedIndex].value">

try this 尝试这个

<select  onchange="location = this.options[this.selectedIndex].value;">
            <option value="multilingual_test.html?lang=en">English</option>
            <option value="multilingual_test.html?lang=ko">한국어</option>
        </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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