0
我想提交一個表單並獲得使用Ajax的響應,但是當我提交表單時,打開一個新窗口並輸入值我打開
該函數可以工作並執行它應該,它只是新的窗口,這是問題
下面是HTML代碼:Ajax post在提交後打開一個新窗口
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="chat_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<div id = "refresh" class="refresh"></div>
<form method="POST" action="save.php" name="chat_send" onsubmit="sendChatData(); return false;">
<input id="sender" name="sender" type="hidden" value ="<?php echo $sender ?>">
<?php echo "$sender:" ?> <input name="texta" type="text" id="texta"/>
<input name="submit" type="submit" value="Send" />
</form>
而且JS代碼:
function sendChatData() {
var xmlHttpReq = false;
var self = this;
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', 'save.php' , true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
document.getElementById("texta").innerHTML = "";
if (self.xmlHttpReq.readyState == 4) {
var x = self.xmlHttpReq.responseText;
}
}
self.xmlHttpReq.send(getquerystring());
}
function getquerystring() {
var qstr = "message=";
try {
qstr += document.getElementById("texta").value;
window.open(qstr);
} catch (e) {
// empty...
}
return qstr;
}
OMG!謝謝...不能相信我是那麼愚蠢 – SagiLow