这次给大家带来怎样让jquery回车触发按钮事件,让jquery回车触发按钮事件的注意事项有哪些,下面就是实战案例,一起来看一下。
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<title>jquery回车触发按钮事件</title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(function () {
$('#submit').click(function () {
var account = $('#accountinput').val();
var password = $('#passwordinput').val();
if (account == '') {
alert('please input account.');
$('#accountinput').focus();
return false;
}
if (password == '') {
alert('please input password.');
$('#passwordinput').focus();
return false;
}
if (account == 'chad' && password == '123456') {
alert('login success.');
}
else {
alert('login failed.');
}
});
$(document).keydown(function (event) {
if (event.keycode == 13) {
$('#submit').triggerhandler('click');
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<p>
<table>
<tr>
<td> account</td>
<td><input id="accountinput" type="text" style="width: 150px;" /></td>
</tr>
<tr>
<td>password</td>
<td><input id="passwordinput" type="text" style="width: 150px;" /></td>
</tr>
<tr>
<td><input id="submit" type="button" value="submit"/></td>
</tr>
</table>
</p>
</form>
</body>
</html>
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
better-scroll插件使用详解(附代码)
iview自定义验证关键词输入框实现方法
以上就是怎样让jquery回车触发按钮事件的详细内容。