如何通过单击链接而不是提交按钮来进行AJAX请求

如何通过单击链接而不是提交按钮来进行AJAX请求? 我希望一旦点击链接从输入字段POST数据

$('selector').click(function(e){ e.preventDefault(); $.ajax({ url: "", type: "POST",//type of posting the data data: , 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 }); }); 

以下是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); }); }); 

您可以使用JQuery和表单序列化function

 $('#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 } }); }); 

用jQuery

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