jQuery передает селектор щелчка в оператор IF - PullRequest
0 голосов
/ 04 февраля 2011

Я пытаюсь создать функцию 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 ]

1 голос
/ 04 февраля 2011

То, что вы хотите сделать, это изменить вашу 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;
});

И вы можете расширить это, добавив другую переменную, чтобы сказать, что делать при отмене.

0 голосов
/ 04 февраля 2011

Попробуйте это:

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