我想通过Ajax请求THR结果停止提交表单... 这是code我尝试使用: 该功能chreg()应返回从Ajax请求给予一个布尔值,但是它没有!
I'd like to stop a form submitting by thr result of an ajax request... this is the code i tried to use: The function chreg() should return a boolean given from the ajax request, but it doesn't!
JavaScript的:
Javascript:
function chreg() {
user = document.getElementById('nome').value;
passw1 = document.getElementById('passw1').value;
passw2 = document.getElementById('passw2').value;
mai = document.getElementById('mail').value;
dat = 'use='+encodeURIComponent(user)+'&pas='+encodeURIComponent(passw1)+'&pas2='+encodeURIComponent(passw2)+'&mail='+encodeURIComponent(mai);
htp = new XMLHttpRequest;
htp.onreadystatechange = function(){
if (htp.readyState == 4 && htp.status == 200){
if (htp.responseText == "true"){
alert('true');
return true;
}
else {
document.getElementById('er2').innerHTML = '<br/><h3>'+tag_esc(decodeURIComponent(htp.responseText))+'</h3>';
return false;
}
}
};
a = Math.random
htp.open("POST","checklog.php?a="+a,true);
htp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
htp.send(dat);
}
和HTML:
And the HTML:
<form action="reg.php" method="POST" onsubmit="return chreg(this);">
<table id="tab2"><tr><td>
<label for="user1">Username: <input type="text" name="user" id="nome" /></label></td> <td>
<label for="mail">Indirizzo e-mail: <input type="text" name="mail" id="mail" /></label> </td>
</tr>
<tr><td><label for="pass1">Password: <input type="password" name="pass1" id="passw1" /> </label></td>
<td><label for="pass2">Password (conferma): <input type="password" name="pass2" id="passw2" /></label></td></tr>
<tr><td colspan="2"><br /><input type="submit" class="st" value="Registrati" /></td> </tr>
</table>
<div id="er2"></div>
</form>
不过,这是行不通的! 帮帮我 非常感谢!
But it doesn't work! Help Thanks a lot!
推荐答案
您将无法使其工作,因为它代表,因为 AJAX是的同步的默认。这意味着,你的 chreg
方法启动该请求并返回的马上的,之前该请求已经完成,其结果成为众所周知。这是一个问题,因为你可以从 chreg
结果前不会返回是已知的,因为你需要的结果,要知道的什么的返回。
You won't be able to make it work as it stands because AJAX is asynchronous by default. That means that your chreg
method initiates the request and returns immediately, before the request has completed and its result become known. This is a problem because you cannot return from chreg
before the result is known since you need that result to know what to return.
一个解决办法是通过改变这里所述第三参数,以使所述请求同步的:
One solution would be to make the request synchronous by changing the third parameter here:
// you will not need to set onreadystatechange at all
htp.open("POST","checklog.php?a="+a,false); // false = synchronous
htp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
htp.send(dat);
if (htp.responseText == "true"){
alert('true');
return true;
}
else {
document.getElementById('er2').innerHTML = '<br/><h3>'+tag_esc(decodeURIComponent(htp.responseText))+'</h3>';
return false;
}
这并不是因为它可能最终锁定UI的请求,这是不希望的持续时间的最佳解决方案。然而,回到异步模式(不存在这个问题),将是更复杂,因为它需要你做的东西倒退:
This is not the best solution because it might end up locking up the UI for the duration of the request, which is undesirable. However, going back to the asynchronous model (which does not have this problem) is going to be more involved because it requires you to do stuff "backwards":
功能 chreg
被调用,使得AJAX请求;它的总是的回报假
(这样的形式不提交)
当你安装了 onreadystatechanged
处理程序检测到了AJAX完成后,检查响应。
如果响应是逻辑真(的形式实际上需要提交),然后使用 form.submit
做到这一点。
Function chreg
is called and makes the AJAX request; it always returns false
(so the form is not submitted)
When the onreadystatechanged
handler you installed detects that the AJAX is complete, it checks the response.
If the response is logically true (the form actually needs to be submitted) then use form.submit
to do it.相关推荐
最新文章