我有一个相当简单的登录表单,提交了一个jQuery AJAX请求。目前,提交它的唯一方法是按“登录”按钮,但我希望能够在用户按下“Enter”时提交表单。jQuery/AJAX登录表单提交输入
我只使用jQuery AJAX请求完成表单提交,但我不确定需要做什么修改才能在用户按下“Enter”时提交表单。
HTML:
<form>
<label for="username">Username</label>
<input type="text" id="username" placeholder="Username" />
<label for="password">Password</label>
<input type="text" id="password" placeholder="Password" />
</form>
<button id="login">Login</button>
的JavaScript:
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username AND password = :password");
$stmt->execute(array(
':username' => $user,
':password' => $pass
));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$affected_rows = $stmt->rowCount();
if ($affected_rows == 1) {
//add the user to our session variables
$_SESSION['username'] = $username;
echo ('Correct');
} else {
echo 'Incorrect username/password';
}
移动的按钮元素到窗体中,并加入到它的类型属性值为“提交” –