zoukankan      html  css  js  c++  java
  • 把功能写在方法里,函数化,方法化

    main函数

    var grade = "<!--{$grade}-->";
            //var grade = 0;
            var lesson_id = "<!--{$lesson_id}-->";
            //var lesson_id = 0;
            if(grade!=""&&lesson_id!=""){
                getBookByGradeAndLesson(grade,lesson_id);
            }
            
            var book_id = "<!--{$aQuestions.book_id}-->";
            var chapter_id = "<!--{$aQuestions.chapter_id}-->";
            var section_id = "<!--{$aQuestions.section_id}-->";
            if(book_id!=""){
                getChapterByBookId(book_id);
                if(chapter_id!=""){
                    $("#chapter_id").find("option[value="+chapter_id+"]").attr("selected",true);
                    getSectionByChapterId(chapter_id);
                    if(section_id!=""){
                        $("#section_id").find("option[value="+section_id+"]").attr("selected",true);
                    }
                }
                $("#book_id").find("option[value="+book_id+"]").attr("selected",true);
            }
            
            //年级变动
            $("#grade").change(function(){
                var grade = $(this).val();
                var lesson_id = $("#lesson_id").val();
                if(grade!=""&&lesson_id!=""){
                    getBookByGradeAndLesson(grade,lesson_id);
                }
                chapterRefresh();
                sectionRefresh();
            });
            
            //科目变动
            $("#lesson_id").change(function(){
                var lesson_id = $(this).val();
                var grade = $("#grade").val();
                if(grade!=""&&lesson_id!=""){
                    getBookByGradeAndLesson(grade,lesson_id);
                }
                chapterRefresh();
                sectionRefresh();
            });
            
            //书变动
            $("#book_id").change(function(){
                var book_id = $(this).val();
                if(book_id!=""){
                    getChapterByBookId(book_id);
                }
                //chapterRefresh();
                sectionRefresh();
            });
            //章变动
            $("#chapter_id").change(function(){
                var chapter_id = $(this).val();
                if(chapter_id!=""){
                    getSectionByChapterId(chapter_id);
                }
            });

    方法体

    function getBookByGradeAndLesson(grade,lesson_id){
                var url = "/default/index/ajax/do/getBookByGradeAndLesson";
                var data = "grade="+grade+"&lesson_id="+lesson_id;
                $.ajax({
                    type:"POST",
                    async:false,
                    url:url,
                    data:data,
                    success:function(response){
                        if(response){
                            $("#book_id").html(response);
                        }else{
                            alert("error");
                        }
                    }
                });
            }
            function getChapterByBookId(book_id){
                var url = "/default/index/ajax/do/getChapterByBookId";
                var data = "book_id="+book_id;
                $.ajax({
                    type:"POST",
                    async:false,
                    url:url,
                    data:data,
                    success:function(response){
                        if(response){
                            $("#chapter_id").html(response);
                        }else{
                            alert("error");
                        }
                    }
                });
            }
            function getSectionByChapterId(chapter_id){
                var url = "/default/index/ajax/do/getSectionByChapterId";
                var data = "chapter_id="+chapter_id;
                $.ajax({
                    type:"POST",
                    async:false,
                    url:url,
                    data:data,
                    success:function(response){
                        if(response){
                            $("#section_id").html(response);
                        }else{
                            alert("error");
                        }
                    }
                });
            }
            function chapterRefresh(){
                var html = "<option vlaue=''>---请选择---</option>";
                $("#chapter_id").html(html);
            }
            function sectionRefresh(){
                var html = "<option vlaue=''>---请选择---</option>";
                $("#section_id").html(html);
            }

    主函数调用方法体里的函数功能块,这样写,使得代码清晰,易修改和维护。

    以后要多多这样写代码,不要把所有的代码都堆成一堆,乱起八糟的。

  • 相关阅读:
    selenium(六)Page Object模式(使用selenium的PageFactory)
    CodeForces 1325C Ehab and Path-etic MEXs(思维)
    CodeForces 1325D Ehab the Xorcist(异或和+算数和)
    家庭房产(模拟)
    取硬币(思维)
    Xor and Sum(异或和+算术和)
    一元三次方程求解(数学、二分)
    最大最小公倍数 (数学、贪心)
    天梯---球队“食物链”(DFS+剪枝)
    HDU-4857 逃生(逆向拓扑排序)
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/3272486.html
Copyright © 2011-2022 走看看