select一:省市区三级联动

<div class="ipt-wrap clearfix">
    <label>籍贯:</label>
    <div class="ipt-r">
        <select class="sele-a" name="province">
            <option value="0">请选择</option>
            <volist name="province" id="p">
                <option value="{pigcms{$p.area_id}">{pigcms{$p.area_name}</option>
            </volist>
        </select>
        <select class="sele-b" name="city">
            <option value="0">请选择</option>

        </select>
        <select class="sele-c" name="area">
            <option value="0">请选择</option>
        </select>
    </div>
</div>

$("select[name='province']").change(function () {
    var pro = $(this).find("option:selected").val();
    $.get("{pigcms{:U('getCity')}", {province: pro}, function (result) {
        $("select[name='city']").empty();
        $("select[name='area']").empty();
        $("select[name='city']").append(new Option('请选择', 0));
        $("select[name='area']").append(new Option('请选择', 0));
        if (result.status == 200) {
            result.data.forEach(function (a) {
                $("select[name='city']").append(new Option(a['area_name'], a['area_id']));
            });
        }
    }, 'json');
});
$("select[name='city']").change(function () {
    var cit = $(this).find("option:selected").val();
    $.get("{pigcms{:U('getArea')}", {city: cit}, function (result) {
        if (result.status == 200) {
            $("select[name='area']").empty();
            result.data.forEach(function (a) {
                $("select[name='area']").append(new Option(a['area_name'], a['area_id']));
            });
        } else {
            $("select[name='area']").empty();
            $("select[name='area']").append(new Option('请选择', 0));
        }
    }, 'json');
});

注意:
forEach()是数组特有的一个方法,只能是数组,对象不行。
posted @ 2020-07-08 14:32  王玉岩  阅读(622)  评论(0编辑  收藏  举报