我只是写了下面的代码来测试是否ENTER键功能仿真的juqery代码工作:jQuery的输入元素模拟的keydown
<html>
<meta http-equiv=Content-Type content="text/html;charset=utf-8">
<script type="text/javascript" src="jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function Test()
{
var e = jQuery.Event("keydown");
e.keyCode = 13;
$("#test_id").trigger(e);
}
</script>
<body>
<div>
<form method="get" id="form" action="http://www.twitter.com">
<input type="text" value="" name="test_id" id="test_id" />
<input type="text" value="" name="test_id1" id="test_id1" />
<button type="submit">go</button>
</form>
<a href="javascript:Test()" id="btn_nfc" ><font size="10">Test</font></a>
</div>
</body>
</html>
如果代码工作正常,那么它会重定向到twitter.com,但实际上,当我点击“测试”按钮时,什么都没有发生,代码中是否有任何错误?
为什么你需要为jQuery的?提交它会触发行动? – ScanQR
@TechBreak是的,提交,这是好的,但也有地方,没有形式或提交,所以实际上我尝试模拟的“Enter” keydown事件 – user440446