2013-07-20 23 views
1

我想要創建一些像Facebook這樣的頁面來加載數據而不更改頁面/頁眉或頁腳中的聊天欄。我知道我可以使用此代碼:如何使鏈接無需使用jquery重新加載所有頁面

$(function() { 
    $("a.load").click(function (e) { 
    e.preventDefault(); 
    $("#content").load($(this).attr("href")); 
    }); 
}); 

但我有一個問題。我想給jQuery附加數據。

<a href="http://mysite.com/profile123" //for example send-more-data="its-profile-link" //and send even more data like: even-more-data="ID:12235663">go to this profile</a> 

,所以我可以做更多的事情與此類似:

if (send-more-data=="its-profile-link") { 
    $id = even-more-data="ID:12235663" //posted on link with jquery 
    mysql_Query("select * from users where id = $id") 
} 
elseif { 
////// 
} 

因此它可能我與ID或類或者出頭發送更多的數據?請給我一個例子。並告訴我使用​​或$.ajax()更好嗎?

+3

使用'$ .ajax' ... – Omar

+0

我認爲你正在尋找這種解決方案,只是你需要根據點擊事件... http://stackoverflow.com/questions/11878365/loading-more-items-from-database-infinite-scroll – Kasma

+0

奧馬爾,請你給我例如$ .ajax? –

回答

3

那麼你可以通過獲得PARAMS進入鏈接:

$(function(){ 
     $("a.load").click(function (e) { 
      e.preventDefault(); 
      $("#content").load($(this).attr("href")+"?data1="+$(this).attr("some-data")+"&data2="+$(this).attr("some-more-data")); 
     }); 
    }); 

OR

可以使用更好的 - 強大的AJAX:

$(function(){ 
      $("a.load").click(function (e) { 
       e.preventDefault(); 
       var link = $(this).attr("href"); 
       var param1 = $(this).attr("some-data"); 
       var param2 = $(this).attr("some-more-data"); 
       /* //$.post example 
       $.post(link, { data1: param1, data2: param2 },function(data) { 
        $("#content").html(data); 
       }); */ 
       //or $.ajax - basically the same thing 
       $.ajax({ 
        url: link, 
        data: { data1: param1, data2: param2 }, 
        success: function(result) { 
        $("#content").html(result); 
        } 
       }); 
      }); 
     }); 

然後在PHP中:

if (@$_REQUEST['data1']=="its-profile-link"){ 
$id = @$_REQUEST['data2']; 
$id = mysql_real_escape_string($id); //or $id = (int)$id; if id is always integer. 
    mysql_Query("select * from users where id = $id") 
} 
elseif{ 
////// 
} 
+0

你可以給我例如$ .ajax嗎? –

+1

@masiharastooyi更新了答案。基本上,這是一回事。 –

+0

所以你是最好的!謝謝 –

0
<a id="my_a" some-data="asd"> 

console.log($('#my_a').attr('some-data')); // asd 

http://api.jquery.com/attr/

+1

雖然這個鏈接可能回答這個問題,但最好在這裏包含答案的重要部分,並提供參考鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – michaelb958

+0

真的,對不起,接受的答案比較好,我也贊成。但是,保持良好的調節工作! – 19greg96

相關問題