jquery weui做的三級聯動
阿新 • • 發佈:2018-12-01
1.引入
<link rel="stylesheet" href="/static/mobile/css/weui.min.css"> <link rel="stylesheet" href="/static/mobile/css/jquery-weui.css"> <link rel="stylesheet" href="/static/mobile/css/style.css"> <script src="/static/mobile/js/jquery.js"></script> <script src="/static/mobile/js/jquery-weui.min.js"></script>
2.頁面佈局
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <%@ include file="../sales/head.jsp" %> <link rel="stylesheet" href="/static/mobile/css/demos.css"> <script src="/static/layuiadmin/layui/layui.js" type="text/javascript"></script> <style> .toolbar .picker-button { color: #04BE02; } </style> </head> <body ontouchstart> <header class='demos-header'> <h1 class="demos-title">Select</h1> </header> <div class="weui-cells weui-cells_form"> <div class="weui-cell"> <div class="weui-cell__hd"><label class="weui-label">選擇省份</label></div> <div class="weui-cell__bd"> <input class="weui-input" id="province" type="text" value=""> </div> </div> <div class="weui-cell"> <div class="weui-cell__hd"><label class="weui-label">選擇城市</label></div> <div class="weui-cell__bd"> <input class="weui-input" id="city" type="text" value=""> </div> </div> <div class="weui-cell"> <div class="weui-cell__hd"><label class="weui-label">選擇縣區</label></div> <div class="weui-cell__bd"> <input class="weui-input" id="county" type="text" value=""> </div> </div> </div> <a href="javascript:;" class="weui-btn weui-btn_primary" style="width: 95%;margin: 20px auto" onclick="submit()">完成</a> </body> </html>
3.js
<script> $.post("/mobile/user/region/selectProvice", function (res) { if (res== null||res==undefined||res.length<=0){ return false; } $("#province").select({ title: "選擇省份", items: res, onChange: function () { var provinceId = $("#province").attr("data-values"); $("#city").attr("data-values", ""); $("#city").attr("value", ""); $("#county").attr("data-values", ""); $("#county").attr("value", ""); if (provinceId==null||provinceId==undefined||provinceId.trim().length<=0){ return false; } $.post("/mobile/user/region/selectCity?provinceId=" + provinceId, function (res) { if (res== null||res==undefined||res.length<=0){ return false; } $("#city").select("update", {items: res,onChange: function () { var cityId= $("#city").attr("data-values"); $("#county").attr("data-values",""); $("#county").attr("value",""); $.post("/mobile/user/region/selectCounty?cityId=" + cityId, function (res) { $("#county").select("update", {items: res}); }) }}); }) } }) }) $("#city").select({ title: "選擇城市", items: [{}] }) $("#county").select({ title: "選擇縣區", items: [{}] }) function submit() { var regionId = $("#county").attr("data-values"); var regionName = $("#county").attr("value"); layui.sessionData('region', []); layui.data('region', { key: 'regionId', value: regionId }); layui.data('region', { key: 'regionName', value: regionName }); location.href = "/mobile/user/index"; } </script>
iteams不能動態變化,加入,用update這個屬性就可以了。需要給input初始化(不然就會變成輸入框)
$("#county").select("update", {items: res});
input初始化 ,若是iteams 為空的話,會報錯
$("#city").select({
title: "選擇城市",
items: [{}]
})
$("#county").select({
title: "選擇縣區",
items: [{}]
})