在我的下拉列表中,我将其设置为 onchange 应该调用 checkval 并传入元素的 id。我刚开始使用非常基本的登录,但甚至无法显示警报。我做错了什么?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script>
function checkval(id){
if($('#' + id).val == "Other")
{
alert("You Selected Other");
//other logic will go here
}
}
</script>
</head>
<body>
<select name="items1[]" id="items1[]" onchange="checkval(id);">
<option selected value="">Select One</option>
<option value="one">One</option>
<option value="Other">Other</option>
</select>
<select name="items2[]" id="items2[]" onchange="checkval(id);">
<option selected value="">Select One</option>
<option value="one">One</option>
<option value="Other">Other</option>
</select>
</body>
</html>
最佳答案
编辑: 似乎它对 id items1[] 失败了。所以更改了标记位。修复了 jsFiddle这里。
请参阅 What characters are allowed in DOM IDs? 的链接
将您的标记更改为 onchange="checkval(this.id);"
。
脚本为
function checkval(id){
if($('#' + id).val() == "Other")
{
alert("You Selected Other");
//other logic will go here
}
}
关于jquery - 调用下拉列表的 OnChange 时事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9039802/