开发者

jQuery on click event send POST request

Trying to send POST request on click event using jQuery with no luck. Here is what I use:

    <script type="text/javascript">
        $('#taxi_update').click(
            $.ajax({
                'type':'POST',
                'data':'id=17446&chru=0',
                'success':function() { ... },
   开发者_C百科             'error':function(){ ... },
                'url':'/url/',
                'cache':false
            })
        );
    </script>
    <a href="#" id="taxi_update">update</a>

Unfortunately it doesn't send any POST request.

Any suggestions what could be wrong with this?


Since your script occurs before the element, it needs to be wrapped in a document.ready event and the click handler itself much be a function, like this:

$(function() {
  $('#taxi_update').click(function() {
    $.ajax({
      type: 'POST',
      data: 'id=17446&chru=0',
      success: function() { ... },
      error: function(){ ... },
      url: '/url/',
      cache:false
    });
  });
});

Most of the time you'll want your code inside a document.ready because the elements it's looking for might not be there/ready yet, like this:

$(function() { });
//or...
$(namedFunction);
//or...
$(document).ready(function() { });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜