jQuery: Capture anchor href onclick and submit asynchronously

旧城冷巷雨未停 提交于 2019-11-29 21:23:42
mOrloff
$('a').click(function(event) { 
    event.preventDefault(); 
    $.ajax({
        url: $(this).attr('href'),
        success: function(response) {
            alert(response);
        }
    });
    return false; // for good measure
});
codingbiz

Try this

$('a').click(function (event) 
{ 
   event.preventDefault(); 

   var url = $(this).attr('href');
   $.get(url, function(data) {
     alert(data);
    });

 });

The problem here is that the Events are not being attached to your element because they are not being bound in DOM ready event . Try including your events in DOM ready event and if it works you will see the alert

<script> 
    $(function() {
         $('a').click(function(event) {
            event.preventDefault();
            alert('fff')
        //here you can also do all sort of things 
        });
    }); 
</script>

After this send the Ajax request and submit the form in the Success callback function..

<script> 
    $(function() {
         $('a').click(function(event) {
             event.preventDefault();
             $.ajax({
                 url: 'url',
                 dataType :'json',
                 data : '{}',
                 success :  function(data){
                     // Your Code here

                     $('#form').submit();
                 }
             })
        });
    }); 
</script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!