前端---二級級聯下拉列表的實現


寫在前面:本是一前端小白,奈何工作需要,不得不硬着頭皮上,但是感覺費勁又慢,光是一個小小的二級級聯的東西就弄個半天,愁!特此總結!

前端代碼:

<select id="select1" onchange="func1()">
        {% for module in module_info %}
            <option   value = "{{module.module}}">{{module.module}}</option>
        {% endfor %}

    </select>

    <select id="select2"  ></select>
<script>

function func1() {
       $.ajax({
           url: '/get_module_field',
           data: {"mydata": $('#select1').val()},
           dataType: "json",
           success: function (output) {
               var fields = output.fields;
               palceHTML ='';
               for (var i=0;i<fields.length;i++){
                   palceHTML += '<option>'+fields[i]+'</option>';
               }
               $('#select2').html(palceHTML);
           },
           error: function () {
               alert('zxcvzx')
           }
       })
    }
</script>

  后端代碼:

@app.route('/get_module_field', methods=['GET', 'POST'])
def get_module_name():
    module_name = request.args.get('mydata')
    print "module_name", module_name
    for i in range(0, len(ModuleName)):
        module_temp_name = ModuleName[i]['module']
        if module_name == module_temp_name:
            temp_dict = ModuleName[i]
            break
    print temp_dict
    return jsonify(temp_dict)

  temp_dict:

{'fields': ['keywords', 'ip', 'log_id'], 'module': 'doorkeeper'}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM