2014-10-08 143 views
-1

我該如何組合這個功能。基本上,如果單擊複選框,則會發生某些情況,並且如果頁面加載並且複選框已被點擊,則第二個函數會發生同樣的情況。將2個功能合併爲1

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