溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

layui實(shí)現(xiàn)select區(qū)域聯(lián)動(dòng)的方法

發(fā)布時(shí)間:2020-06-16 18:52:59 來源:億速云 閱讀:603 作者:元一 欄目:web開發(fā)

簡(jiǎn)介

layui,是一款采用自身模塊規(guī)范編寫的前端 UI 框架,遵循原生 HTML/CSS/JS 的書寫與組織形式,門檻極低,拿來即用。其外在極簡(jiǎn),卻又不失飽滿的內(nèi)在,體積輕盈,組件豐盈,從核心代碼到 API 的每一處細(xì)節(jié)都經(jīng)過精心雕琢,非常適合界面的快速開發(fā)。

要實(shí)現(xiàn)聯(lián)動(dòng)效果注意兩點(diǎn):

第一要可以監(jiān)聽到select的change事件;

第二異步加載的內(nèi)容,需要重新渲染后才可以 正常使用。

Html結(jié)構(gòu):

<div class="x-body">
    <form class="layui-form" action="" method="post">
        <div class="layui-form-item">
            <label class="layui-form-label">選擇地區(qū)</label>
            <div class="layui-input-inline">
                <select name="province" lay-filter="province" id="s_p">
                    <option value="1">請(qǐng)選擇省</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="city" lay-filter="city" id="s_c">
                    <option value="1">請(qǐng)選擇市</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="area" lay-filter="area" id="s_x">
                    <option value="1">請(qǐng)選擇區(qū)/縣</option>
                </select>
            </div>
        </div>
        <div>
            <label class="layui-form-label">選擇地區(qū)</label>
            <div class="layui-input-inline">
                <select name="towns" lay-filter="towns" id="s_t">
                    <option value="1">請(qǐng)選擇鄉(xiāng)鎮(zhèn)/街道</option>
                </select>
            </div>
            <!--<div class="layui-input-inline">-->
                <!--<select name="burg" lay-filter="burg" id="s_b">-->
                    <!--<option value="1">莊/村</option>-->
                <!--</select>-->
            <!--</div>-->
        </div>
    </form>
</div>

js:

<script type="text/javascript">

    layui.use(['form', 'layer', 'laytpl', 'jquery'], function () {
        var form = layui.form
            , $ = layui.jquery;

        var parentId = '0';
        $(function () {
            $.post(serverPath + "sys/area/backProvince/" + parentId, function (result) {
                var p = result;
                for (v in p) {
                    var pp = p[v].id;
                    $("#s_p").append("<option value=" + pp + ">" + p[v].fullname + "</option>")
                }
                form.render();
            })

            form.on('select(province)', function (data) {
                var p = $("#s_p").val();
                if (p != "1") {
                    $.post(serverPath + "sys/area/backProvince/" + p, function (result) {
                        var c = result;
                        $("#s_c").html("");
                        $("#s_c").append("<option value='1'>請(qǐng)選擇市</option>");
                        $("#s_x").html("");
                        $("#s_x").append("<option value='1'>請(qǐng)選擇縣/區(qū)</option>");
                        $("#s_t").html("");
                        $("#s_t").append("<option value='1'>請(qǐng)選擇鄉(xiāng)鎮(zhèn)/街道</option>");
                        $("#s_b").html("");
                        $("#s_b").append("<option value='1'>請(qǐng)選擇村/街道號(hào)</option>");
                        for (v in c) {
                            var cc = c[v].id;
                            $("#s_c").append("<option value=" + cc + ">" + c[v].fullname + "</option>")
                        }
                        form.render();
                    })
                }
            });
            form.on('select(city)', function (data) {
                var c = $("#s_c").val();
                if (c != "1") {
                    $.post(serverPath + "sys/area/backProvince/" + c, function (result) {
                        var x = result;
                        $("#s_x").html("");
                        $("#s_x").append("<option value='1'>請(qǐng)選擇縣/區(qū)</option>");
                        $("#s_t").html("");
                        $("#s_t").append("<option value='1'>請(qǐng)選擇鄉(xiāng)鎮(zhèn)/街道</option>");
                        $("#s_b").html("");
                        $("#s_b").append("<option value='1'>請(qǐng)選擇村/街道號(hào)</option>");
                        for (v in x) {
                            var xx = x[v].id;

                            $("#s_x").append("<option value=" + xx + ">" + x[v].fullname + "</option>")
                        }
                        form.render();
                    });
                }
            });
            form.on('select(area)', function (data) {
                var x = $("#s_x").val();
                if (x != "1") {
                    $.post(serverPath + 'sys/area/backProvince/'+ x, function (result) {
                        var t = result;
                        $("#s_t").html("");
                        $("#s_t").append("<option value='1'>請(qǐng)選擇鄉(xiāng)鎮(zhèn)/街道</option>");
                        $("#s_b").html("");
                        $("#s_b").append("<option value='1'>請(qǐng)選擇村/街道號(hào)</option>");
                        for (v in t) {
                            var xx = t[v].id;
                            $("#s_t").append("<option value=" + xx + ">" + t[v].fullname + "</option>")
                        }
                        form.render();
                    });
                }
            });


        });
    });


</script>

總結(jié):

1、select的chage監(jiān)聽事件使用

form.on('select(myselect)', function(data){})  其中myselect是select的 lay-filter屬性值

2、數(shù)據(jù)異步加載到select的option中之后,點(diǎn)擊該select會(huì)發(fā)現(xiàn)layui的選中效果不起作用,需要使用form.render('select');重新渲染一次,就可以正常使用。

以上就是基于layui實(shí)現(xiàn)select區(qū)域聯(lián)動(dòng)的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注億速云其它相關(guān)文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI