Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在jQuery中向'change'事件添加属性_Javascript_Jquery_Events_Javascript Events - Fatal编程技术网

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)