我试图用ajax解决表单提交,php运行,但表单被卡住了。此函数不运行:ajax.onreadystatechange = function()
btw状态为0,因为控制台说...所以我不知道这里的问题是什么。
我的代码:
<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
_("btnSubmit").disabled = true;
var formdata = new FormData();
formdata.append( "name", _("name").value );
formdata.append( "email", _("email").value );
formdata.append( "phone", _("phone").value );
formdata.append( "officeAddress", _("officeAddress").value );
formdata.append( "officeType", _("officeType").value );
formdata.append( "message", _("message").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "contact.php");
console.log(ajax.status)
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success"){
_("btnSubmit").disabled = false;
$('#myAlert').show('fade');
} else {
_("btnSubmit").disabled = false;
}
}
}
console.log(ajax.status)
ajax.send ( formdata );
}
</script>