form.submit()
功能无法与xhr.onreadystatechange
一起使用。
这是我的登录表单:
<form action="user_home.php" method="post" id="loginForm" >
<tr>
<td colspan=2>Members log in here:</td>
<tr>
<td>Username:</td>
<td><input type='text' name='username' id='username' /></td></tr>
<tr>
<td>Password:</td>
<td><input type='password' name='password' id="password" /></td></tr>
<tr>
<td colspan=2 align='center'>
<input type="submit" name="submit" id="submit" value= "Login" /></td></tr>
</form>
<script src="forLogin.js"></script>
在javascript中
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if ((xhr.status >= 200 && xhr.status < 300) || (xhr.status == 304)) {
if (xhr.responseText == 'VALID') {
alert(xhr.responseText); //this works
var form = document.getElementById('loginForm');
form.submit(); // it doesnt work!!!
} else {
var message = '<h2>Error!</h2><p>The following Error(s) occured: <ul>';
message += '<li class="error">Invalid Username Password combinations!</li>';
showErrorMessage(message);
}
xhr = null;
}
}
};
当我的ajax功能发送 - > echo'VALID';
我收到警报但没有重定向。为什么呢?
有人有任何想法吗?
答案 0 :(得分:0)
问题出在这里,
<input type='text' name='username' id='username' />
<input type='password' name='password' id="password" />
将其更改为: -
<td><input name='username' id='username' type='text' >
<td><input name='password' id="password" type='password' >
现在有效......
答案 1 :(得分:-3)
你可以尝试
document.getElementById("loginForm").submit();
而不是
var form = document.getElementById('loginForm');
form.submit();// it doesnt work!!!