0

我正在尝试使用 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;
}
4

1 回答 1

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;
}
于 2013-07-31T13:22:09.210 回答