通过getElementById查找所选索引是't返回正确的值
Finding the selected index by getElementById isn't returning the right value
"building"下拉列表的选择应等于零。它在第一个选项上。当选择下拉列表旁边的按钮时,将执行函数search()。由于某种原因,警报返回"它不起作用。"
<script>
var a = document.getElementById("building").selectedIndex;
function search() {
if (a === 0) {
window.alert("It worked.");
event.preventDefault();
} else {
window.alert("It didn't work.");
event.preventDefault();
}
}
</script>
这是选择下拉列表。
<form id="apartmentSelection">
<br>
Building:
<select id="building">
<option value="all">All</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
</form>
这是按钮。
<button onclick="search()">Search</button>
Var a
在错误的位置,您不需要event.preventDefault();
两次。
function search() {
event.preventDefault();
var a = document.getElementById("building").selectedIndex;
if(a === 0){
alert("It worked.");
} else {
alert("It didn't work.");
}
}
相关文章:
- 遍历DOM查找字符串有时会正确返回
- 查找大写字母的简单正则表达式是返回小写字母
- 在从索引位置返回的字符串中查找空白
- 在 jquery 选项卡中查找 span 标记并返回该索引
- 解析查询查找方法返回对象而不是数组
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 通过getElementById查找所选索引是't返回正确的值
- navigator.plugins如何查找并返回安装在web浏览器上的插件对象数组
- 如何在日志文件中查找字符串并返回整行
- 正则表达式查找字符串并返回大括号之间的所有内容
- 使用'父母'和'查找'返回undefined-Jquery
- VBScript在目录中查找用户;返回结果(已找到或未找到)
- 如何在数组中查找重复值并返回 True 或 False
- Javascript - 从数组中查找所有类,从类中获取并返回内部文本
- eBay 查找 API findItemsByKeywords 返回“输入 URL 为标头 X-EBAY-SOA-OPE
- CanJS 模型查找全部返回重复项的列表
- JS regex.exec() 返回第一个查找
- 角度:通过数组查找其索引返回对象的内容
- 查找主干集合上的位置仅返回一个结果
- 按值查找嵌套对象并返回查找到的对象键