How Make AJAX REQUEST with clicking in the link no

2019-07-04 05:17发布

How could I make an AJAX REQUEST by clicking on a link instead of a submit button? I want once the link is clicked to POST data from input fields

标签: jquery
4条回答
We Are One
2楼-- · 2019-07-04 05:31

With jQuery

$('#link-selector').on('click', function(event) {
    event.preventDefault();
    $.post('url', {$('form selector').serialize()}, function(json) {
        // proccess results
    }, 'json');
});
查看更多
手持菜刀,她持情操
3楼-- · 2019-07-04 05:32
$('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
  });

});
查看更多
冷血范
4楼-- · 2019-07-04 05:46

Here's how AJAX works:

$('#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);
   });
});
查看更多
贼婆χ
5楼-- · 2019-07-04 05:51

You can user JQuery and the Form serialize functionality

$('#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
        }
    });
});
查看更多
登录 后发表回答