本文介绍了如何检查是否使用AJAX/jQuery单击链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除没有页面刷新的链接,并且我想使用jQuery捕获HTTP请求并将其发送到执行删除操作的特定URL.但是,要使其正常工作,我需要能够使用jQuery/Ajax来获取单击的链接并将其作为http请求发送.

I am trying to delete a link without a page refresh, and I would like to jQuery to capture and send http request to a specific URL that does the deleting. But, for this to work, I need to be able to use jQuery/Ajax to get the clicked link and send it as an http request.

这是链接:

<a href='http://example.com/delete.php?id=243'> <span class='delete'>delete this</span> </a>

现在,这是jQuery和Ajax的树枝模板,用于获取单击的链接,并将带有ID的httprequest发送到delete.php,以便可以删除ID.

Now, this is the jQuery and Ajax in twig template to get the clicked link and send httprequest to delete.php with the id, so that id can be deleted.

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

        }

但是上述功能仅使页面在单击时导航到链接.

But the above function only makes the page navigate to the link when clicked.

推荐答案

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

Prevent default behaviour of link using jQuery's preventDefault().

具有:

<a class='delete' href='http://site.com/delete.php?id=243'> delete this </a>

尝试:

$(".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);
        }
    });
});

这篇关于如何检查是否使用AJAX/jQuery单击链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:32