JQuery方法没有定义

时间:2014-04-12 19:56:05

标签: javascript jquery jquery-mobile

我在JQM(html)网站的主体中有以下JS / JQuery,当我尝试调用sortMethod()时,我得到了' ReferenceError:sortMethod未定义'在FireBug控制台中。

<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" );
            //});
        });

我做错了什么?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在范围$()的外部移动sortMethod,我的意思是sortMethod不需要$()

function sortMethod(){  
....
}