此功能被编写两次,以保存复选框状态以及相应的DIV文本。如何在函数中编写代码,然后分别在加载和单击事件上调用它?

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

                        });      
                });
            });
有帮助吗?

解决方案

您可以将其声明为命名函数,并将其称为两次,这样:

$(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属性在这里工作:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top