我想停止通过 ajax 请求的结果提交的表单...这是我尝试使用的代码:函数 chreg() 应该返回从 ajax 请求给出的布尔值,但它没有!
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:
<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>
但它不起作用!帮助 非常感谢!