gpt4 book ai didi

javascript - 选择要 href 的框值

转载 作者:行者123 更新时间:2023-12-02 15:51:29 25 4
gpt4 key购买 nike

我正在向页面添加多语言功能,但在使用选择框时遇到问题。我想更改页面上动态显示的语言以匹配选择框中所选的语言。

这是我的 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>

单击标签时有效,但单击选择框时无效。标签中使用的 onChange 事件不起作用。我该如何解决这个问题?

最佳答案

您正在使用选择列表。获取值有点复杂。选择列表有许多选项。这些选项是 onchange 事件中 this 对象下的一个数组。

当前选择的索引存储在this.selectedIndex

因此,要获取值,您需要通过所选值访问数组中的正确选项

this.options[this.selectedIndex].value

是从选择列表中获取值的方式。

要修复您的代码,您应该使用

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

您需要获取选项的值

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

关于javascript - 选择要 href 的框值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31827086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com