我有AJAX和jQuery的問題。我編寫登錄系統的函數,但它只能第一次運行。這裏是我的代碼:jQuery和AJAX只能工作一次
的HTML模式:
<form role="form" onsubmit=" return login()" method="post" action="" >
<div class="form-group">
<label for="userName"><span class="glyphicon glyphicon-user"></span>E-mail</label>
<input type="email" name="emailLogin" class="form-control" id="userName" placeholder="e-mail" required>
</div>
<div class="form-group">
<label for="password"><span class="glyphicon glyphicon-eye-open"><span>Password</label>
<input type="password" name="passLogin" class="form-control" id="password" placeholder="Password" required>
</div>
<button type="submit" class="btn btn-success btn-block">Login<span class="glyphicon glyphicon-log-in"></span></button>
</form>
這裏是jQuery的:
function login(){
login=document.getElementById('userName').value;
pass=document.getElementById('password').value;
var dataString="emailLogin="+login+"&passLogin="+pass;
$.ajax({
type: "POST",
url: "models/handler/KlientHandler.php",
cache: false,
data: dataString,
success: function(text){
if(text=='0'){
$("#loginError").removeClass('hidden');
}else{
$("#loginOk").removeClass('hidden');
$("#myModal").modal('hide');
$("#loginLi").html("<a id=\"user\">"+login+" (Profile)<span class=\"glyphicon glyphicon-user\"></span></a>");
$("#regLi").html("<a href=\"logout\" id=\"user\">"+login+" (Logout)<span class=\"glyphicon glyphicon-log-out\"></span></a>");
}
}
});
return false;
}
刪除 '迴歸',只是有的onsubmit = 「登錄()」 – rjustin
@rjustin 當我刪除的回報,並已的onsubmit = 「登錄()」,jQuery的不表明元素與id loginOk/loginError – Pado
我個人會將此按鈕放在onclick =「login()」按鈕上,但是您的問題可能是由於action屬性,它在那裏,但是也是空的。 – rjustin