2013-07-31 58 views
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; 
} 

回答

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; 
} 
+0

OMG!謝謝...不能相信我是那麼愚蠢 – SagiLow

相關問題