将2个函数合并为1

时间:2014-10-08 17:51:48

标签: jquery function

我如何结合这些功能。基本上,当单击复选框时,会发生某些事情,如果页面加载,并且已经单击了复选框,则会发生与第二个函数相同的事情。

        $(document).on("click", ".dimensions", function() {
            var size = $('.td-hide');
            if ($('input#dimensions').is(':checked')) {
            size.show();
            $('.single-select-sm').css('width','148px')
            } else {
            size.hide();
            $('.single-select-sm').css('width','230px')
            }
        });
        $(function() {
            var size = $('.td-hide');
            if ($('input#dimensions').is(':checked')) {
            size.show();
            $('.single-select-sm').css('width','148px')
            } else {
            size.hide();
            $('.single-select-sm').css('width','230px')
            }
        });

1 个答案:

答案 0 :(得分:1)

var myFunc = function()
{
            var size = $('.td-hide');
            if ($('input#dimensions').is(':checked')) {
            size.show();
            $('.single-select-sm').css('width','148px')
            } else {
            size.hide();
            $('.single-select-sm').css('width','230px')
            }
}

$(document).on("click", ".dimensions", function() {
    myFunc();
}

$(function() {
    myFunc();
}