2012-05-24 96 views

回答

1

使用jQuery

$('#link-selector').on('click', function(event) { 
    event.preventDefault(); 
    $.post('url', {$('form selector').serialize()}, function(json) { 
     // proccess results 
    }, 'json'); 
}); 
+0

確定url是什麼,表單選擇器 – choppermio

+0

url是鏈接。選擇器是form/link id或class,就像在css中一樣 – 2012-05-24 09:41:06

9
$('selector').click(function(e){ 
    e.preventDefault(); 
    $.ajax({ 
     url: "<where to post>", 
     type: "POST",//type of posting the data 
     data: <what to post>, 
     success: function (data) { 
     //what to do in success 
     }, 
     error: function(xhr, ajaxOptions, thrownError){ 
      //what to do in error 
     }, 
     timeout : 15000//timeout of the ajax call 
    }); 

}); 
1

您可以將用戶jQuery和表單序列化功能

$('#A-id-selector').click(function() { 
    $.ajax({ 
     type:'POST', 
     url: 'target.url', 
     data:$('#Form-id-selector').serialize(), 
     success: function(response) { 
      // Any code to execute on a successful return 
     } 
    }); 
}); 
2

這裏是AJAX是如何工作的:

$('#link_id').click(function(event){ 
    event.preventDefault(); // prevent default behavior of link click 
    // now make an AJAX request to server_side_file.php by passing some data 
    $.post('server_side_file.php', {parameter : some_value}, function(response){ 
     //now you've got `response` from server, play with it like 
     alert(response); 
    }); 
});