jQuery将单击选择器传递给IF语句

时间:2011-02-03 21:04:22

标签: javascript jquery callback dialog facebox

我正在尝试创建一个jQuery函数来检查facebox对话框上是否按下了“取消”或“删除”按钮,但是我不太清楚如何去做。

现在,我有:

// Confirm and remove group members
$("[id^='removeGroupMember_']").click(function () {
        confirmDialog('Removal', 'Are you sure you want to remove this person from the group?');

        //  I need to check the results of the confirm dialog prior
        //  to calling the code below to remove the actual rows

        $(this).parent().slideUp("fast", function () {
            $(this).remove();
            updateGroupRows();
        });
    return false;
});

confirmDialog的位置:

function confirmDialog(action, message) {
    $.facebox('<h3 class="confirmHeader light tb">Confirm ' + action + '</h3><div class="confirmContent"><p>' + message + '</p><a href="#" id="dialogConfirmAction" class="ras small red button right">' + action + '</a><a href="#" id="dialogConfirmCancel" class="ras small gray button right">Cancel</a></div>');
};

现在,我有两个功能,当按下这些按钮时,但我不知道如何检查他们的结果并将其反馈给我,以便我可以决定是否删除相关的行:

$('#dialogConfirmAction').live('click', function() {
    console.log('Yep... they dun clicked it.');
    return true;
});

$('#dialogConfirmCancel').live('click', function() {
    $.facebox.close();
    return true;
});

非常感谢您提供的任何指导!

2 个答案:

答案 0 :(得分:1)

您要做的是将confirmDialog功能更改为:

function confirmDialog(action, message, actionfunc) {
    $.facebox('<h3 class="confirmHeader light tb">Confirm ' + action + '</h3><div class="confirmContent"><p>' + message + '</p><a href="#" id="dialogConfirmAction" class="ras small red button right">' + action + '</a><a href="#" id="dialogConfirmCancel" class="ras small gray button right">Cancel</a></div>');
    if(actionfunc) {
        $('#dialogConfirmAction').click(actionfunc);
    }
};

然后,您可以通过将函数传递给confirmDialog函数来传递您想要发生的“行动”。这会让你的其他代码看起来像这样:

$("[id^='removeGroupMember_']").click(function () {
    var $that = $(this);
    confirmDialog('Removal', 'Are you sure you want to remove this person from the group?',
                  function() {
                      //This function will be run when the "action" link is clicked
                      $that.parent().slideUp("fast", function () {
                          $(this).remove();
                          updateGroupRows();
                      });
                  });
    return false;
});

您可以通过添加另一个变量来说明取消该怎么做。

答案 1 :(得分:-1)

试试这个:

 // Confirm and remove group members
$("[id^='removeGroupMember_']").click(function () {

        if(confirmDialog('Removal', 'Are you sure you want to remove this person from the group?')) {
            //  I need to check the results of the confirm dialog prior
            //  to calling the code below to remove the actual rows

            $(this).parent().slideUp("fast", function () {
                $(this).remove();
                updateGroupRows();
            });
        }
    return false;
});