作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在向页面添加多语言功能,但在使用选择框时遇到问题。我想更改页面上动态显示的语言以匹配选择框中所选的语言。
这是我的 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/
我是一名优秀的程序员,十分优秀!