Javascript 如何在jQuery中向'change'事件添加属性
我有一个Javascript 如何在jQuery中向'change'事件添加属性,javascript,jquery,events,javascript-events,Javascript,Jquery,Events,Javascript Events,我有一个框: <select id="myselect"> <option value="1">one</option> <option value="2">two</option> <option value="3">three</option> </select> 我知道,在这里我只需要alert$(this.val(),但我的实际示例更复杂,需要e.val。我知道这是可行的,
框:
<select id="myselect">
<option value="1">one</option>
<option value="2">two</option>
<option value="3">three</option>
</select>
我知道,在这里我只需要alert$(this.val()
,但我的实际示例更复杂,需要e.val
。我知道这是可行的,但我无法理解他们的源代码
如何修改事件?我需要做个包装纸什么的吗?任何帮助都将受到感谢
编辑:
我在库上制作一个包装器,它使选择使用e.val
属性,因此我无法修改更改
事件本身。触发事件时,我需要属性已经存在。类似于此,但将其作为默认行为:
e = jQuery.Event('change', {val: 2});
$('#myselect').trigger(e);
我不确定我拿到了,这里有一个镜头:
(function ($){
// keep a copy of the original "select2" function
var lib_select2 = $.fn.select2;
// wrap it into a call which
// first adds a listener to add the value as a property of the event,
// then adds whatever extra stuff the library adds
$.fn.select2 = function(){
this.change(function(e){
e.val = this.value;
});
lib_select2.apply(this, arguments);
}
})(jQuery)
是什么让它变得如此复杂以至于$(this).val()无法工作?@Jay Blanchard,我正在为“select2”做一个包装。它是change
事件期望具有val
属性
(function ($){
// keep a copy of the original "select2" function
var lib_select2 = $.fn.select2;
// wrap it into a call which
// first adds a listener to add the value as a property of the event,
// then adds whatever extra stuff the library adds
$.fn.select2 = function(){
this.change(function(e){
e.val = this.value;
});
lib_select2.apply(this, arguments);
}
})(jQuery)