jquery(或纯js)模拟按下输入键进行测试 [英] jquery (or pure js) simulate enter key pressed for testing

查看:167
本文介绍了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);

这篇关于jquery(或纯js)模拟按下输入键进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆