如果我没有使用正确的术语,请提前道歉。 我获得了一个简单的 sinatra api 来与用户和组一起工作。 我一直在研究这一切的逻辑,虽然一切似乎都正常,但我无法通过 onclick 来调用 issuccess js 函数。我尝试做事件监听器。我现在迷路了,请帮忙。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>HTML5 Login</title>
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<section class="loginform cf">
<form name="login" method="post" onclick="return isSuccess()" accept-charset="utf-8">
<ul>
<li>
<label for="usermail">Email</label>
<input type="email" name="usermail" placeholder="yourname@email.com" required>
</li>
<li>
<label for="password">Password</label>
<input type="password" name="password" placeholder="password" required></li>
<li>
<input type="submit" value= "login" \>
</li>
</ul>
</form>
</section>
<script type="text/javascript">
function isSuccess(){
var firstAdmin = {
"name": "Admin",
"email": "admin@example.com",
"password": "secret",
"admin": true
};
$.post("main.rb", firstAdmin);
function isValidUser() {
$.get( "/users", function(data) {
var x=document.form.usermail.value;
var y=document.form.password.value;
if (x === users.email && y === users.password && users.admin === true){
window.location="userprofile/adminprofile.html";
}
if (x === users.email && y === users.password && users.admin === false){
window.location="userprofile/userprofile.html";
}
alert( "success" );
})
});
}
}
</script>
</body>
</html>
最佳答案
我认为您没有正确使用$get
。你查过它的文档吗?
目前,当您执行以下操作时,您将忽略 get 的结果:
if ($.get('/users') = 200){
因为该函数立即返回(无论 Sinatra API 请求是否已完成)。您需要传入一个回调函数,该函数会在请求实际完成后立即调用:
$.get( "/users", function(data) {
//automatically get here if return status is 200
alert( "success" );
})
关于javascript - html5表单提交到onclick js函数: nothing is happening,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30004758/