开发者

JQuery - Add POST variables to a href link

In a table I have links like this: test.php?id=1

I want to pass prev_id and next_id to test.php as well. (Purpose of these variables is create next/prev buttons; the list can be sorted by the user so no easy way to find out inside test.php.)

However, I do not want prev_id and next_id to be visible in the URL for the user. Can I format the href like this:

<a href="test.php?id=1" prev_id="5" next开发者_运维问答_id="45">

and use JQuery to "hijack" the link, keeping id as GET in the URL, but sending prev_id and next_id to test.php as POST?


Maybe this is your code:

 <a id="link" href="test.php?id=1" prev_id="5" next_id="45">

and the jquery code

$("a#link").click(function(){

    $.post("test.php",
       {
         prev_id: $(this).attr("prev_id"),
         next_id: $(this).attr("next_id")
       },
       function(data) {
         alert("Data Loaded: " + data);
       }
    );

    return false;
});


you can use jquery post

call a function to post your data by onClick event,

 <a onClick="postdata()" prev_id="5" next_id="45">

and the jquery code

function postdata(){
    $.post("test.php", { id: "1", next_id: "2" },
       function(data) {
         alert("Data Loaded: " + data);
       });
}


The easiest way I've found to do this kind of thing is to dynamically add a form to your page then submit it:

$('body').append('<form id="myawesomeform"></form>');
$('#myawesomeform').attr('action','test.php?id=1');
$('#myawesomeform').attr('method','post"');
$('#myawesomeform').append('<input type="hidden" name="prev_id" id="prev_id" value="5">');
$('#myawesomeform').append('<input type="hidden" name="next_id" id="next_id" value="45">')
$('#myawesomeform').submit();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜