2010-08-16 5 views
1

Эта функция записывается дважды, чтобы сохранить состояние флажка, а также соответствующий текст div. Как написать код в функции один раз, а затем дважды вызвать его при загрузке и событиях щелчка?Избегайте дублирования функции в jQuery

$(document).ready(function() { 
        $("#bquote").load("quotes_in.php", function() { 

        // a key prefix is used for the cookie/storage 
        var storedData = getStorage('com_mysite_checkboxes_'); 

        $('div.check input:checkbox').bind('change',function(){ 
        $('#ab_' + this.id).toggle($(this).is(':checked')); 

        // save the data on change 
        storedData.set(this.id, $(this).is(':checked')?'checked':'not'); 
        }).each(function() { 

        // on load, set the value to what we read from storage: 
        var val = storedData.get(this.id); 
        if (val == 'checked') $(this).attr('checked', 'checked'); 
        if (val == 'not') $(this).removeAttr('checked'); 
        if (val) $(this).trigger('change'); 

        }); 
       });     

      }); 



      $(function() { 


      /*load quotes on click of link*/ 
      $("a#main") 
       .click(function() { 
        $(this).addClass("current"); 
        $("#bquote").load("quotes_in.php", function() { 

        // a key prefix is used for the cookie/storage 
        var storedData = getStorage('com_mysite_checkboxes_'); 

        $('div.check input:checkbox').bind('change',function(){ 
        $('#ab_' + this.id).toggle($(this).is(':checked')); 

        // save the data on change 
        storedData.set(this.id, $(this).is(':checked')?'checked':'not'); 
        }).each(function() { 

        // on load, set the value to what we read from storage: 
        var val = storedData.get(this.id); 
        if (val == 'checked') $(this).attr('checked', 'checked'); 
        if (val == 'not') $(this).removeAttr('checked'); 
        if (val) $(this).trigger('change'); 

         });  
       }); 
      }); 

ответ

2

Вы можете объявить его один раз в качестве имени функции и вызвать его в два раза, как это:

$(function() { 
    function loadQuotes() { 
    $("#bquote").load("quotes_in.php", function() { 

     // a key prefix is used for the cookie/storage 
     var storedData = getStorage('com_mysite_checkboxes_'); 

     $('div.check input:checkbox').bind('change',function(){ 
     $('#ab_' + this.id).toggle(this.checked); 

     // save the data on change 
     storedData.set(this.id, this.checked?'checked':'not'); 
     }).each(function() { 

     // on load, set the value to what we read from storage: 
     var val = storedData.get(this.id); 
     if (val == 'checked') $(this).attr('checked', 'checked'); 
     if (val == 'not') $(this).removeAttr('checked'); 
     if (val) $(this).trigger('change'); 

     }); 
    });     
    } 
    $("a#main").click(function() { 
    $(this).addClass("current"); 
    loadQuotes(); 
    }); 
    loadQuotes(); //call it once on load 
}); 

Я также изменил $(this).is(':checked') к this.checked в выше, нет необходимости, чтобы сделать его медленнее, в DOM недвижимость работает здесь :)

+0

спасибо! – input

Смежные вопросы