我正在尝试使用Ajax提交表单并获得响应,但是当我提交表单时,会打开一个新窗口,其中显示我键入的值 该功能工作并做它应该做的事情,它只是新窗口的问题 这是HTML代码:
<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;
}
答案 0 :(得分:3)
在您的代码中,您可以致电window.open
。它会这样做 - 打开一个新的(浏览器)窗口!
function getquerystring() {
var qstr = "message=";
try {
qstr += document.getElementById("texta").value;
window.open(qstr); // <-- Does exactly that - opens a new window!
} catch (e) {
// empty...
}
return qstr;
}