我有我的jQuery输入密钥的问题。对于表单登录回车键是工作物品,但对于表单重置密码,回车键不工作,但点击它即可。jQuery输入密钥不能为其他形式的工作
不工作意味着:如果我按回车键,它将重定向到操作页面,而无需在JS中进行任何验证。
现在我想设置表单重置密码的回车键。
这是到目前为止我的代码:
JS
$(document).ready(function()
{
$(document).bind('keypress', function(e)
{
if (e.keyCode == 13) {
$('#button_sign_in').trigger('click');
}
});
$('#button_sign_in').click(function()
{
if ($("#username").val().length == 0)
{
$('#button_sign_in').attr("disabled", false);
$('#username').focus();
}
else if ($("#password").val().length == 0)
{
$('#button_sign_in').attr("disabled", false);
$('#password').focus();
}
else if ($("#username").val().length == 0 && $("#password").val().length == 0)
{
$('#button_sign_in').attr("disabled", false);
$('#username').focus();
}
else
{
$.ajax
({
url: "login",
type: "post",
data: $('#login_form').serialize(),
dataType:'json',
success: function (data)
{
if (data.status=='SUCCESS')
{
window.location='home.php';
}
else
{
$('#button_sign_in').shake(4,6,700,'#CC2222');
$('#username').focus();
$('#password').val("");
$('#button_sign_in').attr("disabled", false);
$("#info").text("Wrong username or password.");
}
},
error: function (e) {
console.log('error:'+e);
}
});
}
});
$(document).bind('keypress', function(e)
{
if (e.keyCode == 13) {
$('#button_reset').trigger('click');
}
});
$('#button_reset').click(function()
{
if ($("#email").val().length == 0)
{
$('#button_reset').attr("disabled", false);
$('#email').focus();
$("#info2").text("Email is required.");
$('#button_reset').shake(4,6,700,'#CC2222');
}
else
{
$.ajax
({
url: "check_email",
type: "post",
data: $('#reset_form').serialize(),
dataType:'json',
success: function (data)
{
if (data.status=='FOUND')
{
$("#info2").html('<span class="success">Password reset instructions has sent to your email.</span>');
$('#email').val("");
$('#email').focus();
}
else
{
$('#button_reset').shake(4,6,700,'#CC2222');
$('#email').focus();
$('#button_reset').attr("disabled", false);
$("#info2").text("Email is invalid.");
}
},
error: function (e) {
console.log('error:'+e);
}
});
}
});
});
像这样的登录表单的HTML代码:
<form id="login_form">
<div id="info"></div>
<table>
<td><input type="text" name="username" class="input" id="username" placeholder="Username or email"/></td>
<tr>
<td><input type="password" name="password" class="input" id="password" placeholder="Password"/></td>
<tr>
<td><input type="button" id="button_sign_in" class="button_sign_in" value="Sign in"/></td>
<tr>
<td>
<span class="keep_logged_in"><input type="checkbox"> Keep me logged in</span>
</td>
</table>
</form>
和形式复位这样的:
<form id="reset_form" class="collapse">
<div id="info2" class="info2"></div>
<table class="t_info">
<td><input type="text" name="email" class="input" id="email" placeholder="Email"/></td>
<tr>
<td><input type="button" id="button_reset" class="button_reset" value="Submit"/></td>
</table>
</form>
有什么建议吗?也许我的代码有问题?
要绑定两个'keypress'处理程序到“文档”,并且不关注哪个字段他用户在输入时正在输入。 – nnnnnn
嗯..任何例子呢? :) –
与你的JS问题无关(已经有一些很好的答案),但你的HTML也可以做一些更新。例如,你需要确保你的TD标签包裹着相应的TR标签: