![](/img/trans.png)
[英]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.