Javascript 如何在选定的原型javascript选择框中触发onchange事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8455569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to fire onchange event in chosen prototype javascript select box?
提问by Ramesh Kotha
I am using chosen prototype for select box. Now i want to fire onchange event on that select box. here is the link for chosen prototype
我正在使用选定的原型作为选择框。现在我想在那个选择框上触发 onchange 事件。这是所选原型的链接
How to do this. please help me.
这该怎么做。请帮我。
<script type="text/javascript" src="/js/Event.simulate.js"></script>
<script src="/js/prototype.js" type="text/javascript"></script>
<script src="/chosen/chosen.proto.js" type="text/javascript"></script>
<div class="side-by-side clearfix" style="margin-bottom:14px;">
<select data-placeholder="Select Loacation" class="chzn-select" tabindex="2" name="source">
<option value="">Select Loacation</option>
<option value="">option1</option>
<option value="">option2</option>
</select>
</div>
Event.observe($(".chzn-select"),'change', function(){
//alert($(".chzn-select").chosen());
})
回答by clockworkgeek
In this line you have used $()
with a class name, which it does not support.
在这一行中,您使用$()
了一个它不支持的类名。
Event.observe($(".chzn-select"),'change', function(){
A class name can be used multiple times so an array is returned from $$()
, here is one way to work with arrays.
一个类名可以多次使用,因此从 返回一个数组$$()
,这是使用数组的一种方法。
$$(".chzn-select").invoke('observe', 'change', function() {
...
});
I haven't used Chosen before; it's instructions say it needs to be setup so you might have to do this outside of the change event.
我以前没有用过 Chosen;它的说明说它需要设置,因此您可能必须在更改事件之外执行此操作。
document.observe('dom:loaded', function() {
$$(".chzn-select").each(function(select) {
new Chosen(select);
});
});
回答by Krik
use the "addEventListener" method on the select box object.
在选择框对象上使用“addEventListener”方法。
EDIT - here's an example:
编辑 - 这是一个例子:
document.getElementById('selecboxid').addEventListener('change', SomeFunction(), false);