jQuery 将点击选择器传递到 IF 语句中

发布于 2024-10-15 22:57:55 字数 1398 浏览 6 评论 0原文

我正在尝试创建一个 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;
});

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

I am trying to create a jQuery function to check whether a Cancel or Remove button was pressed on a facebox dialog, however I am not quite sure how to go about it.

Right now, I have:

// 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;
});

Where confirmDialog is:

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>');
};

Right now, I have two functions for when those buttons are pressed, but I'm not sure how to check their result and feed that back in so I can decide whether or not to delete the associated row:

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

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

Any guidance you can provide is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宁愿没拥抱 2024-10-22 22:57:55

您想要做的是将您的 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;
});

您可以通过添加另一个变量来扩展它,以说明取消时要执行的操作。

What you want to do is change your confirmDialog function to be like so:

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

You can then pass what you want to happen "on action" by passing a function to the confirmDialog function. This would make your other code look like this:

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

And you can extend that by adding another variable to say what to do on cancel.

横笛休吹塞上声 2024-10-22 22:57:55

试试这个:

 // 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;
});

Try this:

 // 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;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文