结合jQuery中的函数

时间:2013-10-02 21:32:02

标签: jquery

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

}

更新:所以我的代码现在工作正常,因为我更改了我的函数名称。谢谢,但现在我只想使用“jQuery(function($){...”一次。我正在使用它两次。如何只使用一个“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 个答案:

答案 0 :(得分: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
    });
});