下拉菜单的更改处理程序工作有点奇怪
这是我所做的:
<html><head><script type="text/javascript">
function loaded(){
var selectElems=document.getElementsByName("select");
for(i=0;i<selectElems.length;i++){
var elem=selectElems[i];
elem.onchange=function(){
alert(elem.selectedIndex);
}
}
} </script></head>
<body onload="loaded()">
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option> </select>
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option></select>
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option></select>
</body></html>
我需要使用 JavaScript 获取元素的选定索引。但我对此有一个问题。 这是我的代码: http://jsfiddle.net/aRMpt/139/
Here is what I have done:
<html><head><script type="text/javascript">
function loaded(){
var selectElems=document.getElementsByName("select");
for(i=0;i<selectElems.length;i++){
var elem=selectElems[i];
elem.onchange=function(){
alert(elem.selectedIndex);
}
}
} </script></head>
<body onload="loaded()">
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option> </select>
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option></select>
<select name="select">
<option>0</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option></select>
</body></html>
I need to get selected index of element with JavaScript. But I had a problem with that.
Here is my code: http://jsfiddle.net/aRMpt/139/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您的
elem
始终指向最后一个select
元素。最简单的解决方案是在处理程序中使用this
而不是 elem 变量 http: //jsfiddle.net/mendesjuan/aRMpt/140/这并不能真正告诉你你的问题是什么。这是一种解决方法(尽管是一个很好的方法)。您的代码不起作用的原因是您的闭包函数
onchange
使用闭包中相同的elem
。当调用onchange
时,elem
指向最后一个select
元素。避免这种情况的一种方法是引入另一个闭包,冻结 onchange 处理程序的 elem http://jsfiddle.net/mendesjuan/aRMpt/142/这是另一个示例,可以帮助您了解上述示例的工作原理。
变量的冻结也可以通过使用 Function.bind 来完成,但这仅适用于较新的浏览器;但是,上面的链接向您展示了如何使其适用于不支持它的浏览器。 http://jsfiddle.net/mendesjuan/aRMpt/143/
The problem is that your
elem
always points to the lastselect
element. The simplest solution is to usethis
in the handler instead of the elem variable http://jsfiddle.net/mendesjuan/aRMpt/140/This doesn't really tell you what your problem is though. It's a workaround (though a good one). The reason your code doesn't work is because your closure function
onchange
uses the sameelem
from the closure. By the time youronchange
is called,elem
points to the lastselect
element. A way to avoid this is to introduce another closure that freezes the elem for your onchange handler http://jsfiddle.net/mendesjuan/aRMpt/142/Here's another example that may help you understand how the above example works.
This freezing of variables could also be accomplished by using Function.bind, but this only works in newer browsers; however, the above link shows you how to make it work for browsers that don't support it. http://jsfiddle.net/mendesjuan/aRMpt/143/