码迷,mamicode.com
首页 > Web开发 > 详细

Js异步级联选择框实践方法

时间:2014-11-26 20:51:04      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   sp   java   for   on   

HTML:

<li>
                <span>所在地区:</span>

                <select name="prov" id="ddl_prov" onchange="event_change_prov(this);" class="required" missingmsg="请选择省">
                    <option value="">请选择省</option>
                </select>


            </li>
            <li>
                <span></span>

                <select name="city" id="ddl_city" onchange="event_change_city(this);" class="required" missingmsg="请选择市">
                    <option value="">请选择市</option>
                </select>

            </li>
            <li>
                <span></span>

                <select name="area" id="ddl_area" class="required" missingmsg="请选择区">
                    <option value="">请选择区</option>
                </select>


            </li>

Javascript:

//省值改变
        function event_change_prov(self) {
            var value = $(self).val();
            if (isEmpty(value)) {
                bindDropdownList("ddl_city", [])
            } else {
                bindArea("ddl_city", value);
            }
        }

        //市值改变
        function event_change_city(self) {
            var value = $(self).val();
            if (isEmpty(value)) {
                bindDropdownList("ddl_area", [])
            } else {
                bindArea("ddl_area", value);
            }
        }

        //绑定下拉框
        function bindDropdownList(targetId, data) {
            $("#" + targetId).html("");
            var html = "";
            if (targetId == "ddl_prov")
            {
                html="<option value=‘‘>请选择省</option>";
            } else if (targetId == "ddl_city") {
                html = "<option value=‘‘>请选择市</option>";
            } else if (targetId == "ddl_area") {
                html = "<option value=‘‘>请选择区</option>";
            } else {
                html = "<option value=‘‘>请选择</option>";
            }
            for (var i = 0; i < data.length; i++) {
                html = html + "<option value=‘" + data[i].Value + "‘>" + data[i].Text + "</option>";
            }
            $("#" + targetId).html(html);
        }

        //级联下拉框列表
        var ddls = ["ddl_prov", "ddl_city", "ddl_area"];

        //绑定地区
        function bindArea(id, code) {
            if (isEmpty(id) || $("#" + id).length == 0) {return;
            }
            if (id == "ddl_prov" && isEmpty(code)) {
                code = "";
            }
            Request.get(‘/XXX/XXX?code=‘ + code,
                    function (result) {
                        bindDropdownList(id, result);
                        var name = $("#" + id).attr("name");
                        var or = $("#hid_" + name).val();
                        if (isEmpty(or)) {return;
                        }
                        $("#" + id).val(or);
                        var ncode = $("#" + id).val();
                        var index = ddls.indexOf(id);
                        if (index >= 0 && index < ddls.length - 1) {
                            var nid = ddls[index + 1];
                            bindArea(nid, ncode);
                        }
                    });
        }

 

Js异步级联选择框实践方法

标签:style   blog   io   ar   color   sp   java   for   on   

原文地址:http://www.cnblogs.com/xachary/p/4124759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!