1

我想创建一些像 facebook 这样的页面来加载数据而不更改页脚中的页面/页眉或聊天栏。我知道我可以使用以下代码:

$(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 的数据吗?请给我一个例子。并告诉我使用.load()or 更好$.ajax()吗?

4

2 回答 2

3

好吧,您可以将获取参数传递给链接:

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

或者

你可以使用更好的——强大的 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{
//////
}
于 2013-07-20T12:50:21.377 回答
0
<a id="my_a" some-data="asd">

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

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

于 2013-07-20T12:49:19.220 回答