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

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

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

  • 相关阅读:
    markdown基本语法
    每天一个Linux命令:pwd(3)
    每天一个Linux命令:cd(2)
    每天一个Linux命令:ls(1)
    每天一个Linux命令:man(0)
    maven命令行创建项目问题
    Regular Expression
    JS事件流
    canvas与svg区别
    js调试
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/3272486.html
Copyright © 2011-2022 走看看