Объединение функций в jQuery

0
jQuery(function ($) {
$('.confirmbttn').click(function (e) {
e.preventDefault();


    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {

    });
});
});

function confirm(message, callback) {
$('.confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});

}


jQuery(function ($) {
$('.confirmbttn2').click(function (e) {
e.preventDefault();


    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {

    });
});
});

function confirm(message, callback) {
$('.confirm').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});

}

UPDATE: Итак, мой код теперь отлично работает, потому что я изменил имя своих функций. Спасибо, но теперь я хотел бы использовать только "jQuery (function ($) {..." once. Я использую его дважды. Как объединить все мои функции, используя только один "jQuery (function ($) {..."

jQuery(function ($) {
    $('.confirmbttn').click(function (e) {
        e.preventDefault();
        execChart1("", function () {
        });
    });
});

function execChart1(message, callback) {
    $('.confirm').modal({
        closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
        position: ["20%",],
        overlayId: 'confirm-overlay',
        containerId: 'confirm-container', 
        onShow: function (dialog) {
            var modal = this;

            $('.message', dialog.data[0]).append(message);

            // if the user clicks "yes"
           $('.yes', dialog.data[0]).click(function () {
                // call the callback
                if ($.isFunction(callback)) {
                    callback.apply();
                }
               // close the dialog
                modal.close(); // or $.modal.close();
            });
        }
    });
}


jQuery(function ($) {
$('.confirmbttn1').click(function (e) {
    e.preventDefault();
    execChart2("", function () {
    });
});
});

function execChart2(message, callback) {
$('.confirm1').modal({
    closeHTML: "<a href='#' title='Close' class='modal-close'>x</a>",
    position: ["20%",],
    overlayId: 'confirm-overlay',
    containerId: 'confirm-container', 
    onShow: function (dialog) {
        var modal = this;

        $('.message', dialog.data[0]).append(message);

        // if the user clicks "yes"
        $('.yes', dialog.data[0]).click(function () {
            // call the callback
            if ($.isFunction(callback)) {
                callback.apply();
            }
            // close the dialog
            modal.close(); // or $.modal.close();
        });
    }
});
}
  • 1
    удалить один экземпляр function confirm () { . может быть только один, второй перекрывает первый.
  • 1
    Вы, вероятно, не должны вызывать вашу функцию confirm() , так как есть стандартный window.confirm() .
Показать ещё 6 комментариев
Теги:

1 ответ

1

Вам нужно привязать локальную переменную к экземпляру, на который было нажато:

$('.confirmbttn, .confirmbttn2').click(function (e) {
    e.preventDefault();
    var theButton = this;

    // example of calling the confirm function
    // you must use a callback function to perform the "yes" action
    confirm("", function () {
        // Do stuff with theButton
    });
});
  • 0
    Спасибо, я попробую это

Ещё вопросы

Сообщество Overcoder
Наверх
Меню