jquery(或纯 js)模拟按下回车键进行测试
问题描述:
模拟用户按下回车"的最佳方式是什么?$(element).keypress() 似乎不允许我传入实际按下的键.
What the best way to simulate the user pressing "enter"? $(element).keypress() doesn't seem to allow me to pass in the actual key that was pressed.
这是用于单元测试.
答
var e = jQuery.Event("keypress");
e.which = 13; //choose the one you want
e.keyCode = 13;
$("#theInputToTest").trigger(e);