我有一个字段在您键入时“纠正”自己.我们编写了一些代码,当按下箭头键时会阻止此功能:
handleKeyUp: function(e) { var arrowKeys = _.range(37,41),key = e.which; // allow user to move cursor by keyboard if ($.inArray(key,arrowKeys) < 0) { this.removeInvalidCharacters(); } },
我们想为此写一个单元测试;但是,我们无法弄清楚如何通过$input.trigger(‘keyup’)传递e.which.我知道触发器的第二个参数允许我们发送“额外选项”;但是,这只会将值传递给handleKeyUp函数.想法?
解决方法
试试这个 –
var e = $.Event("keyup"); e.which = 37; $input.trigger(e);
http://api.jquery.com/category/events/event-object/
As of jQuery 1.6,you can also pass an object to jQuery.Event() and
its properties will be set on the newly created Event object.
// Create a new jQuery.Event object with specified event properties. var e = jQuery.Event("keydown",{ keyCode: 64 }); // trigger an artificial keydown event with keyCode 64 jQuery("body").trigger( e );