jQuery - 从Popup打开href

时间:2014-06-18 14:41:53

标签: jquery hyperlink popup click href

他是我的第一个问题,让我们看看它是怎么回事。

所以,在这里:

HTML:

<td><a href="https://www.google.es" class="confirmDelete">delete</a></td>
<div id="confirmDeleteBox" title="Delete Confirm" style="display:none"></div>

jQuery的:

 $('.confirmDelete').click(OpenDeleteDialog);
//Delete Column functionallity
function OpenDeleteDialog() {
    $("#confirmDeleteBox").html("Confirm Delete Dialog Box");

    $("#confirmDeleteBox").dialog({
        resizable: false,
        modal: true,
        title: "Modal",
        height: 250,
        width: 400,
        buttons: {
            "Yes": function () {
                $(this).dialog('close');
                callback(true);
            },
            "No": function () {
                $(this).dialog('close');
                callback(false);
            }
        }
    });
    event.preventDefault();
}



function callback(value) {
    if (value) {
        $(this).closest('.confirmDelete').attr('href');

    } else {
        alert("Rejected");
    }
}

所以,问题是我有一个包含删除,复制或添加行的列的表,并且我想在每次要删除,复制或添加时都进行弹出确认。 弹出窗口有效,但是当我想按&#34;是&#34;它没有返回我按下的链接(在我的示例中是谷歌链接)。

我正在使用动态链接,所以我不能直接指向,动态生成行(以及它的href)。

我如何&#34;点击&#34; &#34;是&#34;中链接的href;弹出窗口的选项?

顺便说一下,我试过这部分:

function callback(value) {
    if (value) {
        $(this).attr('href');

    } else {
        alert("Rejected");
    }
}

我尝试过:$(this).attr('href');window.location = $(this).attr('href');等等......

2 个答案:

答案 0 :(得分:1)

你有两个问题:

  1. callback()中,this不是您认为的那样。
  2. .attr('something') getter 。它只检索一个值。
  3. 试试这个:

    $('.confirmDelete').click(OpenDeleteDialog);
    //Delete Column functionallity
    function OpenDeleteDialog() {
    
        var $link = $(this); //keep track of the element that was clicked
    
        $("#confirmDeleteBox").html("Confirm Delete Dialog Box");
    
        $("#confirmDeleteBox").dialog({
            resizable: false,
            modal: true,
            title: "Modal",
            height: 250,
            width: 400,
            buttons: {
                "Yes": function () {
                    $(this).dialog('close');
                    callback(true, $link); //pass the reference to the element
                },
                "No": function () {
                    $(this).dialog('close');
                    callback(false);
                }
            }
        });
        event.preventDefault();
    }
    
    function callback(confirmed, $el) {
        if (confirmed) {
            window.location = $el.attr('href'); //change the page's location    
        } else {
            alert("Rejected");
        }
    }
    

答案 1 :(得分:1)

Working Demo


试试这个,

$('.confirmDelete').click(OpenDeleteDialog);
//Delete Column functionallity
var currentHref;   //store href of current selected link

function callback(value) {
    if (value) {
        window.location = $(currentHref).attr('href');

    } else {
        alert("Rejected");
    }
}

function OpenDeleteDialog(event) {

    currentHref = event.target;  //get the clicked element

    $("#confirmDeleteBox").html("Confirm Delete Dialog Box");

    $("#confirmDeleteBox").dialog({
        resizable: false,
        modal: true,
        title: "Modal",
        height: 250,
        width: 400,
        buttons: {
            "Yes": function () {
                $(this).dialog('close');
                callback(true);
            },
            "No": function () {
                $(this).dialog('close');
                callback(false);
            }
        }
    });
    event.preventDefault();
}

  • 使用event.target获取当前链接的值。
  • 您还可以使用

    传递当前所选元素的值
    $('.confirmDelete').click(function(){
        OpenDeleteDialog(this);
    );
    
    function OpenDeleteDialog(obj){..}