如何检查是否使用AJAX / jQuery单击了一个链接

我试图删除没有页面刷新的链接,我想jQuery捕获并发送http请求到执行删除的特定URL。 但是,为了实现这一点,我需要能够使用jQuery / Ajax来获取被点击的链接并将其作为http请求发送。

这是链接:

 delete this  

现在,这是twig模板中的jQuery和Ajax获取点击链接并将httprequest发送到带有id的delete.php,以便可以删除id。

 $(document).ready(function() { $(".delete").click(function(){ $.ajax({ type: "GET", url: "{{ path('http://example.com/delete.php?id=243'}}", cache: "false", dataType: "html", success: function(result){ $(".success").append(result); } }); }); } 

但是上述function只会使页面在单击时导航到链接。

你需要preventDefault

  $(".delete").click(function(e){ e.preventDefault() $.ajax({ .. 

我不知道我是否让你在这里,但这可能对你有帮助: http : //api.jquery.com/event.preventDefault/

 $( "a" ).click(function( event ) { event.preventDefault(); //your code } 

使用jQuery的preventDefault()防止链接的默认行为。

有:

  delete this  

尝试:

 $(".delete").click(function(e){ e.preventDefault(); $.ajax({ type: "GET", url: "{{ path('http://site.com/delete.php?id=243'}}", cache: "false", dataType: "html", success: function(result){ $(".success").append(result); } }); });