簡體   English   中英

提交后,Ajax帖子會打開一個新窗口

[英]Ajax post opens a new window after submitting

我正在嘗試使用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;
}

在您的代碼中,您可以調用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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM