2014-04-12 14 views
0

我有以下的JS/JQuery的在我的JQM(HTML)網站的身體,當我嘗試調用sortMethod()我得到「的ReferenceError:sortMethod沒有定義」在Firebug控制檯。JQuery的方法

<script> 
     $(function(){ 
      $("[name=radio-choice-h-3]").change(function() { 
       //alert('Selected: '+$('input[name=radio-choice-h-3]:checked').val()); 
       sessionStorage.sortBy = $('input[name=radio-choice-h-3]:checked').val(); 
       sortMethod(); 
      }); 
     }); 
     $(function(){ 
      sortMethod(); 
     });  
     $(function sortMethod(){  
      if(sessionStorage.sortBy === "model" || sessionStorage.sortBy == null || sessionStorage.sortBy.trim()) 
      { 
       theManufacturers('model'); 
       $(document).on("pagecontainerbeforeshow", function() { 
        var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
        $("#radio-choice-h-2a", page).prop("checked", true).checkboxradio("refresh"); 
       }); 
      } 
      else if(sessionStorage.sortBy === "year") 
      { 
       theManufacturers('year'); 
       $(document).on("pagecontainerbeforeshow", function() { 
        var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
        $("#radio-choice-h-2b", page).prop("checked", true).checkboxradio("refresh"); 
       }); 
      } 
      else if(sessionStorage.sortBy === "location") 
      { 
       theManufacturers('location'); 
       $(document).on("pagecontainerbeforeshow", function() { 
        var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
        $("#radio-choice-h-2c", page).prop("checked", true).checkboxradio("refresh"); 
       }); 
      } 
      else if(sessionStorage.sortBy === 'ttaf') 
      { 
       theManufacturers("ttaf"); 
       $(document).on("pagecontainerbeforeshow", function() { 
        var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
        $("#radio-choice-h-2d", page).prop("checked", true).checkboxradio("refresh"); 
       }); 
      } 
     }); 

     $(function theManufacturers(inputSearch){ 
      var qryString = 0; 

      //set up string for adding <li/> 
      var li = ""; 
      var jqxhr = $.getJSON("url", 
      function(data){ 
        $.each(data.items, function(i,item){ 
         li+='<li><a href="#" data-transition="slidedown"><img src="' + item.Image.trim() + '" style="height:80px;/> <span style="font-size:0.75em;">' + item.Manufacturer.trim() + ' ' + item.Model.trim() + '(' + item.Price.trim() + ')</span><br/><span style="font-size:0.65em;">S/N: ' + item.Serial.trim() + ' | TTAF: ' + item.TTAF.trim() + ' | LOC: ' + item.Location.trim() + '</span><br/><span style="font-size:0.65em;">' + item.DealerName.trim() + '</span></a></li>'; 
        }); 

        $("#results-list").append(li); 
        $("#results-list").listview("refresh"); 
       }); 
      //jqxhr.done(function() { 
      // console.log("second success"); 
      //}); 
     }); 

我在做什麼錯?

回答

1
<script> 
      //first you define your functions, they will not be executed "from alone" 
    //syntax:|function name() { .. code } 
      function sortMethod(){  
       if(sessionStorage.sortBy === "model" || sessionStorage.sortBy == null || sessionStorage.sortBy.trim()) 
       { 
        theManufacturers('model'); 
        $(document).on("pagecontainerbeforeshow", function() { 
         var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
         $("#radio-choice-h-2a", page).prop("checked", true).checkboxradio("refresh"); 
        }); 
       } 
       else if(sessionStorage.sortBy === "year") 
       { 
        theManufacturers('year'); 
        $(document).on("pagecontainerbeforeshow", function() { 
         var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
         $("#radio-choice-h-2b", page).prop("checked", true).checkboxradio("refresh"); 
        }); 
       } 
       else if(sessionStorage.sortBy === "location") 
       { 
        theManufacturers('location'); 
        $(document).on("pagecontainerbeforeshow", function() { 
         var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
         $("#radio-choice-h-2c", page).prop("checked", true).checkboxradio("refresh"); 
        }); 
       } 
       else if(sessionStorage.sortBy === 'ttaf') 
       { 
        theManufacturers("ttaf"); 
        $(document).on("pagecontainerbeforeshow", function() { 
         var page = $.mobile.pageContainer.pagecontainer("getActivePage"); 
         $("#radio-choice-h-2d", page).prop("checked", true).checkboxradio("refresh"); 
        }); 
       } 
      }; 


      //same here, just a defined function 
      function theManufacturers(inputSearch){ 
       var qryString = 0; 


       var li = ""; 
       var jqxhr = $.getJSON("url", 
       function(data){ 
         $.each(data.items, function(i,item){ 
          li+='<li><a href="#" data-transition="slidedown"><img src="' + item.Image.trim() + '" style="height:80px;/> <span style="font-size:0.75em;">' + item.Manufacturer.trim() + ' ' + item.Model.trim() + '(' + item.Price.trim() + ')</span><br/><span style="font-size:0.65em;">S/N: ' + item.Serial.trim() + ' | TTAF: ' + item.TTAF.trim() + ' | LOC: ' + item.Location.trim() + '</span><br/><span style="font-size:0.65em;">' + item.DealerName.trim() + '</span></a></li>'; 
         }); 

         $("#results-list").append(li); 
         $("#results-list").listview("refresh"); 
        }); 

      }); 


      //here is an example how i outsourced your changehandler to a function 
      function initializeChangeHandler(){ 
       $("[name=radio-choice-h-3]").change(function() { 
        //alert('Selected: '+$('input[name=radio-choice-h-3]:checked').val()); 
        sessionStorage.sortBy = $('input[name=radio-choice-h-3]:checked').val(); 
       }); 
      }; 


      //heres the function that will be executed when the document model is complete loaded 
    //syntax:|$(function(){ .. code .. }) 
      $(function(){ // as the code inside here is executed, you can now call your functions 
        initializeChangeHandler();// <<-- example for understanding 
        sortMethod(); // <<-- heres the call 
      }); 


</script> 

跟上這種結構(初始化你之前定義的東西)的 那麼你可以確定你的功能已定義; 9

+0

我沒有期待你爲我重寫這一切。謝謝。 –

+0

得到快速打字手指,你很受歡迎; -D –

+0

但反應慢? ;-) –

0

移動sortMethod在外部範圍$(),我的意思是sortMethod不需要用$()

function sortMethod(){ 
.... 
}