2015-03-03 103 views
0

下面的代碼僅在我取消註釋警報時起作用。這段代碼有什麼問題。
temp.htmljquery load()僅適用於alert()

<script src="jquery.js" type="text/javascript"></script> 
    <div id="show"></div> 
    <form id="chat_form"> 
     <input type="submit" /> 
    </form> 
    <div id="abc"></div> 
    <script> 
     $(document).ready(function() { 
      $("#chat_form").submit(function (event) { 
       $("#show").load("test.html", function (responseTxt, statusTxt, xhr) { 
        if (statusTxt == "success") 
         $("#abc").text(responseTxt); 
        if (statusTxt == "error") 
         $("#abc").text(xhr.statusText); 
        //alert(xhr.statusText); 

       }); 
      }); 
     }); 

    </script> 

的test.html

<p>hi i am test.html</p> 
+0

嘗試阻止默認表單提交操作 - http://jsfiddle.net/arunpjohny/upfjLurm/1/ – 2015-03-03 08:05:26

回答

0

您應該防止形式來得到提交:

$("#chat_form").submit(function (event) { 
    event.preventDefault(); // <-----add this to stop the form submission. 
+0

然後警報如何解決問題,因爲警報是在ajax回調..所以到時候ajax請求完成表格應該有submited ...除非它是一個同步阿賈克斯請求不是它 – 2015-03-03 08:07:20

+0

@ArunPJohny我猜想警報保持屏幕在那一點,OP是能夠看到在該點的文本在該點的時間,但當註釋出來的形式只是被提交,沒有什麼在div中。我想這是問題。 – Jai 2015-03-03 08:11:57

0

使用此

$("#chat_form").submit(function (event) { 
    event.stopPropagation(); 
});