[英]I have a form with the submit button linked to javascript function but nothing is happening when I click the submit button
[英]html5 form submit to onclick js function: nothing is happening
如果我使用的术语不正确,请提前道歉。 我得到了一个简单的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" );
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.