2016-03-28 76 views
2

我有一個index.php頁面的鏈接阿賈克斯開放如下:通行證鏈路ID到PHP頁面與另一個DIV

<a id="link" href="test.php?id=1">Test</a> 

我有相同的頁面上的div容器如下:

同一頁上
<div id="container" class="panel-body"> 

和AJAX jQuery代碼如下:

<script type="text/javascript"> 
$("#link").click(function(){ 
    $.ajax({ 
    type: "get", 
     url: "test.php", 
     data: {'data':dataString}, 
     success: function(data){ 
      $('#container').html(data); 
     } 
    }); 
}); 
</script> 

我的工作是通過上述ID = 1中的鏈接test.php的一個nd獲取值顯示在div #container中的同一頁面上。

問題是點擊鏈接打開一個新的頁面在一個新的窗口與URL test.php?ID = 1,而不是顯示在同一頁上的PHP的內容。

如何在jquery中顯示test.php頁面的結果在div中命名爲#container的同一頁?

在此先感謝。

+1

的可能的複製[jQuery的禁用鏈接](http://stackoverflow.com/questions/970388/jquery-disable -a-link) – Scimonster

回答

1

問題是,點擊鏈接在新窗口中打開一個新頁面的URL test.php的ID = 1而不是顯示?同一頁面上的php內容。

你需要停止錨的默認行爲與event.preventDefault()

$("#link").click(function(e) { 
    e.preventDefault(); // <-------stop the def behavior here. 
    var id = this.href.split('=').pop(); // extract the id here 
    $.ajax({ 
    type: "get", 
    url: "test.php", 
    data: {id:id}, // now pass it here 
    success: function(data) { 
     $('#container').html(data); //copy and paste for your special case 
    } 
    }); 
}); 
+0

謝謝....你的代碼工作得很好。 – user3790186

+0

歡迎您@ user3790186。 – Jai

0

只是阻止鏈接的默認行爲:

$("#link").click(function(event){ 
    event.preventDefault(); 

    $.ajax({ 
    type: "get", 
     url: "test.php", 
     data: {'data':dataString}, 
     success: function(data){ 
      $('#container').html(data); //copy and paste for your special case 
     } 
    }); 
}); 
+0

現在它顯示在firebug中,因爲dataString沒有定義 data:{'data':dataString}, – user3790186

+0

是的,但是最初的問題解決了吧? – smnbbrv

相關問題