如果从对话框确认,则Jquery加载单击的链接

时间:2011-08-27 21:58:23

标签: jquery jquery-ui-dialog

我有这个jquery代码:

    $("#deletec-box").dialog({
    autoOpen:false,
    resizable:false,
    height:230,
    modal:true,
    buttons:{
        "Confirm":function(){
            window.location=$("a[id*='deletec-confirm']").attr('href');
            $(this).dialog("close");
        },Cancel:function(){
            $(this).dialog("close");
        }
    }
});

$("a[id*='deletec-confirm']").click(function(){
    $("#deletec-box").dialog("open");
    return false;
});

在网页上我有:

<a href="?action=delc&cid=2" id="deletec-confirm2" title="Delete This Record">Delete</a>
<a href="?action=delc&cid=3" id="deletec-confirm3" title="Delete This Record">Delete</a>

当您点击上面的第二个链接时,它会使用第一个链接网址加载。如何根据上面点击的链接获取jquery对话框以获取正确的URL?我想要做的就是如果他们点击要求确认的删除链接,如果他们点击对话框中的确认按钮我想要他们最初点击的网址继续。

1 个答案:

答案 0 :(得分:2)

我有两点建议:

  1. 使用jQuery data API
    这里的想法是将适当的信息存储在已知位置,并在对话框中使用它。例如:

    $("#deletec-box").dialog({
        autoOpen:false,
        resizable:false,
        height:230,
        modal:true,
        buttons:{
            "Confirm":function(){
                window.location=$('#deletec-box').data('loc');
                $(this).dialog("close");
            },Cancel:function(){
                $(this).dialog("close");
            }
        }
    });
    
    $("a[id*='deletec-confirm']").click(function(){
        $('#deletec-box').data('loc', $(this).attr('href'));
        $("#deletec-box").dialog("open");
        return false;
    });
    
  2. 使用适当的按钮在需要时修改对话框。这可能过于冗长,而且工作量超过了它的价值以及创建大量对象的开销。

    $("#deletec-box").dialog({
        autoOpen:false,
        resizable:false,
        height:230,
        modal:true,
    });
    
    $("a[id*='deletec-confirm']").click(function(){
        loc = $(this).attr('href');
        $('#deletec-box').dialog({
            buttons:{
                    "Confirm":function(){
                        window.location=loc;
                        $(this).dialog("close");
                    },Cancel:function(){
                        $(this).dialog("close");
                    }
            }
        });
        $("#deletec-box").dialog("open");
        return false;
    });