Javascript选择下拉菜单onchange无法正确触发 [英] Javascript select drop down menu onchange not firing properly

查看:172
本文介绍了Javascript选择下拉菜单onchange无法正确触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的onchange不能正确触发javascript方法.

my onchange isn't firing the javascript method properly.

选择代码(在HTML文件中)

Select code (in HTML file)

<select id="crit_1" onchange="crit1Update()">
    <option value=null>Criteria 1</option>
 <option value="major">Major</option>
 <option value="grad">Graduation Year</option>
</select>

crit1Update()代码(与包含上述内容的HTML文件位于同一文件夹中的外部filter.js文件,其中以下代码位于HTML头

crit1Update() code (in external filter.js file in same folder as HTML file containing the above with the following code in head of HTML

<script type="text/javascript" src="filter.js">
</script>

function crit1Update() {
 var crit1 = document.criteria.getElementsByID("crit1");
 var criteria1 = crit1.options[crit1.selectedIndex].value;
 alert("criteria1"); // this is never firing, so this isnt getting called
 if (criteria1 == null) {
  // eventually set all other lists to null in here as well
  filter(null,null,null,null,null,null)
            // the above returns all the resumes 
            // (this is eventually going to filter resumes)
 }
 else if (criteria1 == "major") {
  alert("Major!");
 }
}

我是否缺少任何明显的错误?我不知道为什么没有调用alert("criteria1".我来自Java背景,这是我第一次涉足JS,所以我可能缺少一些简单的东西.

Are there any glaring or not glaring errors that I am missing? I dont see why the alert("criteria1" isnt being called. I come from a java background and this is my first foray into JS, so I may be missing something simple.

感谢您的帮助.

推荐答案

您的<select> ID是"crit_1",并且您的函数正在寻找ID"crit1"(无下划线).

Your <select> ID is "crit_1" and your function is looking for ID "crit1" (no underscore).

最好将<select>元素作为对该函数的引用,例如

It would probably be a better idea to pass the <select> element as a reference to the function, eg

JavaScript

JavaScript

function crit1Update(crit1) {
    var criteria1 = crit1.options[crit1.selectedIndex].value;

HTML

<select id="crit1" name="crit1" onchange="crit1Update(this)">

这篇关于Javascript选择下拉菜单onchange无法正确触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆