省市联动JS能绑定数据库数据么??能绑定的话怎么调数据库数据!!!!
6条回答 默认 最新
- Go 旅城通票 2015-04-28 01:52关注
省select可以直接绑定数据,市生成js数组,通过js获取数据中进行option的添加删除
一个简单的DEMO,注意数据的生成
省:<select id="pro"><option value="">请选择省</option> <option value="1">北京</option> <option value="2">上海</option> <option value="3">广西</option></select> 市:<select id="city"><option value="">请选择市</option></select> <script> var arrCity = []; arrCity[1] = [{ t: '北京市', id: 1}]; arrCity[2] = [{ t: '上海市', id: 2}]; arrCity[3] = [{ t: '南宁市', id: 3 }, { t: '桂林市', id: 4 }, { t: '柳州市', id: 5}]; document.getElementById('pro').onchange = function () { addOptions(document.getElementById('city'), arrCity[this.value]); } function addOptions(s, arr, initValue) { if (!arr || arr.length == 0) arr = [{ t: '请选择市', id: ''}]; if (!s) { alert('select对象不存在!'); return false } s.options.length = 0; var selectedIndex = 0; for (var i = 0; i < arr.length; i++) { s.options.add(new Option(arr[i].t, arr[i].id)); if (arr[i].id == initValue) selectedIndex = i; } } </script>
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 素材场景中光线烘焙后灯光失效
- ¥15 请教一下各位,为什么我这个没有实现模拟点击
- ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
- ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
- ¥20 有关区间dp的问题求解
- ¥15 多电路系统共用电源的串扰问题
- ¥15 slam rangenet++配置
- ¥15 有没有研究水声通信方面的帮我改俩matlab代码
- ¥15 ubuntu子系统密码忘记
- ¥15 保护模式-系统加载-段寄存器