如何使用json数据创建下拉列表

时间:2015-03-18 07:40:40

标签: javascript php jquery json

我已从mysql表中提取数据并现在解码json_decode 我们有董事会,班级,科目的json数组,它们像父母一样相关 - > child-> grandchild(board-> class-> subject) board_auto_id是classList json数组的第一个class_auto_id是classSubjectList的第一个索引

  

注意:我发布了classList和classSubjectList

的部分数组
var boardList           =[{"board_auto_id":"1","board_name":"CBSE"},{"board_auto_id":"2","board_name":"ICSE"},{"board_auto_id":"3","board_name":"NCERT"}];
var classList          = {"1":[{"class_auto_id":"1","class_name":"VI"},{"class_auto_id":"2","class_name":"VII"},{"class_auto_id":"3","class_name":"VIII"},{"class_auto_id":"4","class_name":"IX"},{"class_auto_id":"5","class_name":"X"},{"class_auto_id":"6","class_name":"XI"},{"class_auto_id":"7","class_name":"XII"}]};
var classSubjectList   = {"1":[{"class_auto_id":"1","sub_auto_id":"1","subject_name":"Science"},{"class_auto_id":"1","sub_auto_id":"2","subject_name":"Mathematics"},{"class_auto_id":"1","sub_auto_id":"3","subject_name":"Geography : The Earth Our Habitat "},{"class_auto_id":"1","sub_auto_id":"4","subject_name":"History : Our Pasts - I"},{"class_auto_id":"1","sub_auto_id":"5","subject_name":"Civics : Social And Political Life-I"},{"class_auto_id":"1","sub_auto_id":"86","subject_name":"English : Grammar"},{"class_auto_id":"1","sub_auto_id":"139","subject_name":"English : Writing Skills"},{"class_auto_id":"1","sub_auto_id":"155","subject_name":"English : Reading"},{"class_auto_id":"1","sub_auto_id":"209","subject_name":"संस्कृत : व्याकरण"},{"class_auto_id":"1","sub_auto_id":"220","subject_name":"Computer Science"},{"class_auto_id":"1","sub_auto_id":"235","subject_name":"Literature in English ( NCERT)"},{"class_auto_id":"1","sub_auto_id":"238","subject_name":"हिंदी व्याकरण"},{"class_auto_id":"1","sub_auto_id":"253","subject_name":"English : Vocabulary"}],"2":[{"class_auto_id":"2","sub_auto_id":"6","subject_name":"Science"},{"class_auto_id":"2","sub_auto_id":"7","subject_name":"Mathematics"},{"class_auto_id":"2","sub_auto_id":"8","subject_name":"Geography : Our Environment"},{"class_auto_id":"2","sub_auto_id":"9","subject_name":"History : Our Pasts - II"},{"class_auto_id":"2","sub_auto_id":"10","subject_name":"Civics : Social And Political Life - II"},{"class_auto_id":"2","sub_auto_id":"87","subject_name":"English : Grammar"},{"class_auto_id":"2","sub_auto_id":"140","subject_name":"English : Writing Skills"},{"class_auto_id":"2","sub_auto_id":"154","subject_name":"English : Reading"},{"class_auto_id":"2","sub_auto_id":"210","subject_name":"संस्कृत : व्याकरण"},{"class_auto_id":"2","sub_auto_id":"213","subject_name":"Computer Science"}]}
  

HTML代码

<select name="boardId" id="boardId" class="style1"><option value="">Select Board</option></select>
<select name="classId" id="classId" class="style1"><option value="">Select Class</option></select>
<select name="subjectId" id="subjectId" class="style1"><option value="">Select Subject</option></select>

2 个答案:

答案 0 :(得分:1)

您可以执行类似这样的操作将json绑定到下拉列表

<select id="boardList"></select>

  $.each(boardList, function (key, value) {
             console.log(value.board_auto_id);
             appenddata += "<option value = '" + value.board_auto_id + " '>" + value.board_name + " </option>";                        
         });
        $('#boardList').html(appenddata);
OnChange下拉列表的{p> boardList您需要从另一个jsonArray中过滤记录并将其绑定到与上面代码中提到的相同的下一个dropDown。

答案 1 :(得分:0)

使用

显示第一块显示板
jQuery.each(boardList, function(i , item) {
       selectedBoard='';// if you display no board selected 
       boardListHtml += '<option '+ selectedBoard +'   value="'+item.board_auto_id+'">'+item.board_name+'</option>';
    });

jQuery('#boardId').html(boardListHtml);

为每次调用创建类和主题的函数

function getBoardByClass(boardId){
    jQuery('#classId').html('');
    var classChapterListHtml = '<option value="">Select Class</option>';
    jQuery.each(classList, function(i , item) {
       var selectedClass = (boardId ==item.class_auto_id) ? 'selected' : '';    
       classChapterListHtml += '<option '+ selectedClass +'   value="'+item.class_auto_id+'">'+item.class_name+'</option>';
    });
    jQuery('#classId').html(classChapterListHtml);

}


function getSubjectByClass(classID){

    jQuery('#subjectId').html('');
    var subjectListHtml = '<option value="">Select Subject</option>';
    jQuery.each(classSubjectList, function(id , subjectList) {
            if (classID==id) {
            jQuery.each(subjectList, function(subIds , subjectName) {

                subjectListHtml += '<option value="'+subjectName.sub_auto_id+'">'+subjectName.subject_name+'</option>';

                });

            }

    });

    jQuery('#subjectId').html(subjectListHtml);

}

最后更改了课程和主题过滤器的事件

$("#boardId").change(function() {
        jQuery('#classId').html('');
        getBoardByClass($(this).val());

        });

$("#classId").change(function() {
        jQuery('#subjectId').html('');
        getSubjectByClass($(this).val());

        });