如何在HTML中使用if语句(JavaScript)和select标记
How to use the if statement (JavaScript) with the select tag in HTML?
我已经尝试了很多方法,但这是不对的:
<html>
<head>
<title>Select</title>
</head>
<body>
<select name="test">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
</select>
<script>
if (test=="two") {
document.write("two!");
}
</script>
</body>
</html>
我想让一些东西出现在屏幕上,如果用户选择" 2 ",但这不起作用
我想这可能对你有帮助
<select name="test" id="mySelect" onchange="myFunction()">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
</select>
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("mySelect").value;
if(x == "two"){
document.getElementById("demo").innerHTML = "You selected: " + x;
}
}
</script>
看一看http://jsfiddle.net/snsbbytw/
我猜你必须听select的change事件
document.getElementsByName("test")[0].addEventListener("change", function(e) {
if (e.target.value === "two") { // not sure if it's e.targe.value, and this might not work in all browsers
document.write("two!");
}
}
您可能正在寻找onChange事件
<select name="test" onchange="console.log(this.value)">
避免使用document。写入,因为它将替换文档输出。当然,避免使用内联脚本;)
相关文章:
- 如何将php内容放入javascript标记中
- 如何使用javascript标记句子
- 同一网页中有多个javascript标记
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- 我应该把我的胡子模板脚本标记像javascript标记一样放在正文的底部吗
- javascript标记数组在googlemapsapi3中是未定义的
- javascript标记化器
- javascript标记中包含变量的AngularJS JSON数组
- 我可以在javascript中打开php标记,在php标记中打开javascript标记吗
- Javascript标记问题
- 谷歌地图JavaScript标记问题
- 使用 javascript 标记图像上的区域
- 使用 JavaScript 标记列表中的单词
- Javascript 标记化列表到关联集合
- 在子页中嵌入javascript标记
- javascript标记equivant中的node.js require()
- 使用javascript标记当前菜单项
- 在SelectRails页面的Header中使用Javascript标记
- 将JavaScript标记为安全
- 如何在javascript标记中编写php代码