根据所选的下拉列表输出浏览器文本

时间:2014-06-03 07:52:33

标签: javascript html

<script type="text/javascript">
Running = "For beginner to professional marathon runners";
Paragliding = "For all those interested in the extreme sport of paragliding";
Swimming = "For all levels of swimming enthusiasts";
function dropdownTip(value){  
document.getElementById("result").innerHTML = value;
}</script>

我正在尝试根据用户选择的类别从变量输出文本。如何将“value”更改为名为的变量字符串?或者有更好的方法吗?

<form action="submit.php" method="post">
<select onChange="dropdownTip(this.value)" select name="input"> 
<option value="Athletics:">Athletics:</option>
<option value="Running">Running</option>
<option value="Paragliding">Paragliding</option>
<option value="Swimming">Swimming</option>
</select> 
<input type="Submit" value="Next">
</form>
<!-- Text output here !-->
<div id="result"></div>

见解赞赏。

1 个答案:

答案 0 :(得分:1)

如果我们认为HTML标记是固定的,那么JavaScript代码需要将字符串(如“Running”)映射到其他字符串(例如“For beginner to professional marathon runner”)。这最好使用一个对象来完成作为属性的第一类字符串和作为属性值的第二类字符串:

var explanation = {
  Athletics: "For atheletes",
  Running: "For beginner to professional marathon runners",
  Paragliding: "For all those interested in the extreme sport of paragliding",
  Swimming: "For all levels of swimming enthusiasts"
}
function dropdownTip(value){  
  document.getElementById("result").innerHTML = explanation[value];
}