用jQuery实现省市区三级联动的代码方法
最编程
2024-02-01 14:06:32
...
最近项目需要用到关于省市区三级联动下拉选择的功能,于是乎网上搜了一些做法,觉得有一些只是给出了小的案例,却很难找到详细的省、市、区的具体数据(其实,用baidu搜索就是这样啦),果断用google,搜出来的博文质量相当高,特此记录记录!!!
对于这个效果,其实我发现主要在于两点:1、jquery的筛选遍历操作;2、存储省、市、区这些数据时候的格式。另外一点是如何将获取得到的数据放到select option中(即下拉框中!)
对于第一个问题的解决,其实熟悉Jquery的博友估计是不难的,主要涉及:find,eq,attr等函数的操作。下面是其中涉及到的一个案例:用于获取省的所有数据,并将其放到下拉框中:
function func_suc_getXmlProvice(xml) { //jquery的查找功能 var sheng = $(xml).find("prov"); //jquery的遍历与查询匹配 eq功能,并将其放到下拉框中 sheng.each(function(i) { province.append("<option value=" + i + ">" + sheng.eq(i).attr("text") + "</option>"); }); }
下面进入正题,建立一个动态web工程,然后将下面讲到的html、js文件、存放省市区xml文件 都放在Web-Contetn下即可。首先看一看前端html文件province_city_select.html:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>省市区三级联动</title> <script type="text/javascript" src="jquery-1.8.3.min.js"></script> <script type="text/javascript" src="province_city.js"></script> </head> <body> <select id="province" name="province"> </select> <select id="city" name="city"> </select> <select id="area" name="area"> </select> <div> 地址是:<span id="pro_city"></span> <input id="txtProCity" type="text" /> </div> </body> </html>
然后注意放进jquery-1.8.3.min.js,可以来我这里下来:jquery库文件。然后需要新建province_city.js,其源码如下:
$(function() { var address = $("#pro_city"); var province = $("#province"); var city = $("#city"); var area = $("#area"); var preProvince = "<option value=\"\">选择省(市)</option>"; var preCity = "<option value=\"\">选择市(区)</option>"; var preArea = "<option value=\"\">选择区(县)</option>"; //初始化 province.html(preProvince); city.html(preCity); area.html(preArea); //文档加载完毕:即从province_city_select_Info.xml获取数据,成功之后采用 //func_suc_getXmlProvice进行 省的 解析 $.ajax({ type : "GET", url : "province_city_select_Info.xml", success : func_suc_getXmlProvice }); //省 下拉选择发生变化触发的事件 province.change(function() { //province.val() : 返回是每个省对应的下标,序号从0开始 if (province.val() != "") { city.html(preCity); //根据下拉得到的省对于的下标序号,动态从从province_city_select_Info.xml获取数据,成功之后采用 //func_suc_getXmlProvice进行省对应的市的解析 $.ajax({ type : "GET", url : "province_city_select_Info.xml", success : func_suc_getXmlCity }); } }); //市 下拉选择发生变化触发的事件 city.change(function() { area.html(preArea); $.ajax({ type : "GET", url : "province_city_select_Info.xml", //根据下拉得到的省、市对于的下标序号,动态从从province_city_select_Info.xml获取数据,成功之后采用 //func_suc_getXmlArea进行省对应的市对于的区的解析 success : func_suc_getXmlArea }); }); //区 下拉选择发生变化触发的事件 area.change(function() { var value = province.find("option:selected").text() + city.find("option:selected").text() + area.find("option:selected").text(); address.text(value); $("#txtProCity").val(value); }); //解析获取xml格式文件中的prov标签,得到所有的省,并逐个进行遍历 放进下拉框中 function func_suc_getXmlProvice(xml) { //jquery的查找功能 var sheng = $(xml).find("prov"); //jquery的遍历与查询匹配 eq功能,并将其放到下拉框中 sheng.each(function(i) { province.append("<option value=" + i + ">" + sheng.eq(i).attr("text") + "</option>"); }); } function func_suc_getXmlCity(xml) { var xml_sheng = $(xml).find("prov"); var pro_num = parseInt(province.val()); var xml_shi = xml_sheng.eq(pro_num).find("city"); xml_shi.each(function(j) { city.append("<option value=" + j + ">" + xml_shi.eq(j).attr("text") + "</option>"); }); } function func_suc_getXmlArea(xml) { var xml_sheng = $(xml).find("prov"); var pro_num = parseInt(province.val()); var xml_shi = xml_sheng.eq(pro_num).find("city"); var city_num = parseInt(city.val()); var xml_xianqu = xml_shi.eq(city_num).find("county"); xml_xianqu.each(function(k) { area.append("<option value=" + k + ">" + xml_xianqu.eq(k).attr("text") + "</option>"); }); } });
然后,重点来了,当然是province_city_select_Info.xml里面的内容啦,因为比较多,我只贴出一部分,如下所示,其余的可以到我这里来下载:省市区三级数据
<prov id="130000" text="河北省"> <city id="130100" text="石家庄市"> <county id="130102" text="长安区"></county> <county id="130103" text="桥东区"></county> <county id="130104" text="桥西区"></county> <county id="130105" text="新华区"></county> <county id="130107" text="井陉矿区"></county> <county id="130108" text="裕华区"></county> <county id="130121" text="井陉县"></county> <county id="130123" text="正定县"></county> <county id="130124" text="栾城县"></county> <county id="130125" text="行唐县"></county> <county id="130126" text="灵寿县"></county> <county id="130127" text="高邑县"></county> <county id="130128" text="深泽县"></county> <county id="130129" text="赞皇县"></county> <county id="130130" text="无极县"></county> <county id="130131" text="平山县"></county> <county id="130132" text="元氏县"></county> <county id="130133" text="赵县"></county> <county id="130181" text="辛集市"></county> <county id="130182" text="藁城市"></county> <county id="130183" text="晋州市"></county> <county id="130184" text="新乐市"></county> <county id="130185" text="鹿泉市"></county> </city> <city id="130200" text="唐山市"> <county id="130202" text="路南区"></county> <county id="130203" text="路北区"></county> <county id="130204" text="古冶区"></county> <county id="130205" text="开平区"></county> <county id="130207" text="丰南区"></county> <county id="130208" text="丰润区"></county> <county id="130223" text="滦县"></county> <county id="130224" text="滦南县"></county> <county id="130225" text="乐亭县"></county> <county id="130227" text="迁西县"></county> <county id="130229" text="玉田县"></county> <county id="130230" text="唐海县"></county> <county id="130281" text="遵化市"></county> <county id="130283" text="迁安市"></county> </city> <city id="130300" text="秦皇岛市"> <county id="130302" text="海港区"></county> <county id="130303" text="山海关区"></county> <county id="130304" text="北戴河区"></county> <county id="130321" text="青龙满族自治县"></county> <county id="130322" text="昌黎县"></county> <county id="130323" text="抚宁县"></county> <county id="130324" text="卢龙县"></county> </city> <city id="130400" text="邯郸市"> <county id="130402" text="邯山区"></county> <county id="130403" text="丛台区"></county> <county id="130404" text="复兴区"></county> <county id="130406" text="峰峰矿区"></county> <county id="130421" text="邯郸县"></county> <county id="130423" text="临漳县"></county> <county id="130424" text="成安县"></county> <county id="130425" text="大名县"></county> <county id="130426" text="涉县"></county> <county id="130427" text="磁县"></county> <county id="130428" text="肥乡县"></county> <county id="130429" text="永年县"></county> <county id="130430" text="邱县"></county> <county id="130431" text="鸡泽县"></county> <county id="130432" text="广平县"></county> <county id="130433" text="馆陶县"></county> <county id="130434" text="魏县"></county> <county id="130435" text="曲周县"></county> <county id="130481" text="武安市"></county> </city> <city id="130500" text="邢台市"> <county id="130502" text="桥东区"></county> <county id="130503" text="桥西区"></county> <county id="130521" text="邢台县"></county> <county id="130522" text="临城县"></county> <county id="130523" text="内丘县"></county> <county id="130524" text="柏乡县"></county> <county id="130525" text="隆尧县"></county> <county id="130526" text="任县"></county> <county id="130527" text="南和县"></county> <county id="130528" text="宁晋县"></county> <county id="130529" text="巨鹿县"></county> <county id="130530" text="新河县"></county> <county id="130531" text="广宗县"></county> <county id="130532" text="平乡县"></county> <county id="130533" text="威县"></county> <county id="130534" text="清河县"></county> <county id="130535" text="临西县"></county> <county id="130581" text="南宫市"></county> <county id="130582" text="沙河市"></county> </city> <city id="130600" text="保定市"> <county id="130602" text="新市区"></county> <county id="130603" text="北市区"></county> <county id="130604" text="南市区"></county> <county id="130621" text="满城县"></county> <county id="130622" text="清苑县"></county> <county id="130623" text="涞水县"></county> <county id="130624" text="阜平县"></county> <county id="130625" text="徐水县"></county> <county id="130626" text="定兴县"></county> <county id="130627" text="唐县"></county> <county id="130628" text="高阳县"></county> <county id="130629" text="容城县"></county> <county id="130630" text="涞源县"></county> <county id="130631" text="望都县"></county> <county id="130632" text="安新县"></county> <county id="130633" text="易县"></county> <county id="130634" text="曲阳县"></county> <county id="130635" text="蠡县"></county> <county id="130636" text="顺平县"></county> <county id="130637" text="博野县"></county> <county id="130638" text="雄县"></county> <county id="130681" text="涿州市"></county> <county id="130682" text="定州市"></county> <county id="130683" text="安国市"></county> <county id="130684" text="高碑店市"></county> </city> <city id="130700" text="张家口市"> <county id="130702" text="桥东区"></county> <county id="130703" text="桥西区"></county> <county id="130705" text="宣化区"></county> <county id="130706" text="下花园区"></county> <county id="130721" text="宣化县"></county> <county id="130722" text="张北县"></county> <county id="130723" text="康保县"></county> <county id="130724" text="沽源县"></county> <county id="130725" text="尚义县"></county> <county id="130726" text="蔚县"></county> <county id="130727" text="阳原县"></county> <county id="130728" text="怀安县"></county> <county id="130729" text="万全县"></county> <county id="130730" text="怀来县"></county> <county id="130731" text="涿鹿县"></county> <county id="130732" text="赤城县"></county> <county id="130733" text="崇礼县"></county> </city> <city id="130800" text="承德市"> <county id="130802" text="双桥区"></county> <county id="130803" text="双滦区"></county> <county id="130804" text="鹰手营子矿区"></county> <county id="130821" text="承德县"></county> <county id="130822" text="兴隆县"></county> <county id="130823" text="平泉县"></county> <county id="130824" text="滦平县"></county> <county id="130825" text="隆化县"></county> <county id="130826" text="丰宁满族自治县"></county> <county id="130827" text="宽城满族自治县"></county> <county id="130828" text="围场满族蒙古族自治县"></county> </city> <city id="130900" text="沧州市"> <county id="130902" text="新华区"></county> <county id="130903" text="运河区"></county> <county id="130921" text="沧县"></county> <county id="130922" text="青县"></county> <county id="130923" text="东光县"></county> <county id="130924" text="海兴县"></county> <county id="130925" text="盐山县"></county> <county id="130926" text="肃宁县"></county> <county id="130927" text="南皮县"></county> <county id="130928" text="吴桥县"></county> <county id="130929" text="献县"></county> <county id="130930" text="孟村回族自治县"></county> <county id="130981" text="泊头市"></county> <county id="130982" text="任丘市"></county> <county id="130983" text="黄骅市"></county> <county id="130984" text="河间市"></county> </city> <city id="131000" text="廊坊市"> <county id="131002" text="安次区"></county> <county id="131003" text="广阳区"></county> <county id="131022" text="固安县"></county> <county id="131023" text="永清县"></county> <county id="131024" text="香河县"></county> <county id="131025" text="大城县"></county> <county id="131026" text="文安县"></county> <county id="131028" text="大厂回族自治县"></county> <county id="131081" text="霸州市"></county> <county id="131082" text="三河市"></county> </city> <city id="131100" text="衡水市"> <county id="131102" text="桃城区"></county> <county id="131121" text="枣强县"></county> <county id="131122" text="武邑县"></county> <county id="131123" text="武强县"></county> <county id="131124" text="饶阳县"></county> <county id="131125" text="安平县"></county> <county id="131126" text="故城县"></county> <county id="131127" text="景县"></county> <county id="131128" text="阜城县"></county> <county id="131181" text="冀州市"></county> <county id="131182" text="深州市"></county> </city> </prov>
好了,介绍一下效果:
刚开始的:
下拉选择省的,然后出现市的,选择了市的,然后出现了区的,最后选择区的时候,得到地址:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
[姿势估计] 实践记录:使用 Dlib 和 mediapipe 进行人脸姿势估计 - 本文重点介绍方法 2):方法 1:基于深度学习的方法:。 基于深度学习的方法:基于深度学习的方法利用深度学习模型,如卷积神经网络(CNN)或递归神经网络(RNN),直接从人脸图像中学习姿势估计。这些方法能够学习更复杂的特征表征,并在大规模数据集上取得优异的性能。方法二:基于二维校准信息估计三维姿态信息(计算机视觉 PnP 问题)。 特征点定位:人脸姿态估计的第一步是通过特征点定位来检测和定位人脸的关键点,如眼睛、鼻子和嘴巴。这些关键点提供了人脸的局部结构信息,可用于后续的姿势估计。 旋转表示:常见的旋转表示方法包括欧拉角和旋转矩阵。欧拉角通过三个旋转角度(通常是俯仰、偏航和滚动)描述头部的旋转姿态。旋转矩阵是一个 3x3 矩阵,表示头部从一个坐标系到另一个坐标系的变换。 三维模型重建:根据特征点的定位结果,三维人脸模型可用于姿势估计。通过将人脸的二维图像映射到三维模型上,可以估算出人脸的旋转和平移信息。这就需要建立人脸的三维模型,然后通过优化方法将模型与特征点对齐,从而获得姿势估计结果。 特征点定位 特征点定位是用于检测人脸关键部位的五官基础部分,还有其他更多的特征点表示方法,大家可以参考我上一篇文章中介绍的特征点检测方案实践:人脸校正二次定位操作来解决人脸校正的问题,客户在检测关键点的代码上略有修改,坐标转换部分客户见上图 def get_face_info(image). img_copy = image.copy image.flags.writeable = False image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) results = face_detection.process(image) # 在图像上绘制人脸检测注释。 image.flags.writeable = True image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) box_info, facial = None, None if results.detections: for detection in results. for detection in results.detections: mp_drawing.Drawing.detection = 无 mp_drawing.draw_detection(image, detection) 面部 = detection.location_data.relative_keypoints 返回面部 在上述代码中,返回的数据是五官(6 个关键点的坐标),这是用 mediapipe 库实现的,下面我们可以尝试用另一个库:dlib 来实现。 使用 dlib 使用 Dlib 库在 Python 中实现人脸关键点检测的步骤如下: 确保已安装 Dlib 库,可使用以下命令: pip install dlib 导入必要的库: 加载 Dlib 的人脸检测器和关键点检测器模型: 读取图像并将其灰度化: 使用人脸检测器检测图像中的人脸: 对检测到的人脸进行遍历,并使用关键点检测器检测人脸关键点: 显示绘制了关键点的图像: 以下代码将参数 landmarks_part 添加到要返回的关键点坐标中。
-
用 jQuery + css 实现蓝色横向二级导航菜单效果的代码
-
用jQuery实现自定义图片热点效果的方法
-
用JavaScript实现的联动广告代码示例
-
轻松用jQuery实现MD5加密的方法
-
用jQuery实现省市区三级联动的代码方法
-
使用ASP.NET MVC2和JQuery实现下拉列表的三级联动功能
-
Grid++Report 锐浪报表开发常见问题解答集锦-报表设计 问:怎样在设计时打印预览报表? 答:为了及时查看报表的设计效果,Grid++Report 报表设计应用程序提供了四种查看视图:普通视图、页面视图、预览视图与查询视图。通过窗口下边的 Tab 按钮可以在四种视图中任意切换。在预览视图中查看报表的打印预览效果,在查询视图中查看报表的查询显示效果。如果在报表的记录集提供了数据源连接串与查询 SQL,在进入预览视图与查询视图时会利用数据源连接串与查询 SQL 从数据源中自动取数,否则 Grid++Report 将自动生成模拟数据进行模拟打印预览与查询显示。注意:在预览视图与查询视图中看到的报表运行结果有可能与在你程序中的最终运行结果有差异,因为在报表的生成过程中我们可以在程序中对报表的生成行为进行一定的控制。 问:怎样用 Grid++Report 设计交叉表? 答:Grid++Report 没有提供专门实现交叉表的功能,其它的报表构件提供的交叉表功能一般也比较死板和功能有限。利用 Grid++Report 的编程接口可以做出灵活多变,功能丰富的交叉表。示例程序 CrossTab 就是一个实现交叉表的例子程序,认真领会此例子程序,你就可以做出自己想要各种交叉表,并能提取一些共用代码,便于重复使用。 问:怎样设置整个报表的缺省字体? 答:设置报表主对象的字体属性,也就是设置了整个报表的缺省字体。如果改变报表主对象的字体属性,则没有专门的设置字体属性的子对象的字体属性也跟随改变。同样每个报表节与明细网格也有字体属性,他们的字体属性也就是其拥有的子对象的缺省字体。 问:怎样在打印时限制一页的输出行数? 答:设定明细网格的内容行的‘每页行数(RowsPerPage)’属性即可。另外要注意‘调节行高(AdjustRowHeight)’属性值:为真时根据页面的输出高度自动调整行的高度,使整个页面的输出区域充满。为假时按设计时的高度输出行。 问:怎样显示中文大写金额? 答:将对象的“格式(Format)”属性设为 “$$” 及可,可以设置格式的对象有:字段(IGRField)、参数(IGRParameter)、系统变量(IGRSystemVarBox)与综合文字框(IGRMemoBox),其中综合文字框是在报表式上设格式。 问:能否实现自定义纸张与票据打印? 答:Grid++Report 完全支持自定义纸张的打印,只要在报表设定时在页面设置中选定自定义纸张,并指定准确的纸张尺寸。当然要在最终输出时得道合适的打印结果,输出打印机必须支持自定义纸张打印。Windows2000/XP/2003 操作系统上可以在打印机上定义自定义纸张,也可以采用这种方式实现自定义纸张打印。 问:怎样实现 0 值不打印? 答:直接设置格式串就可以,在“数字格式”设置对话框中选定“0 不显示”,就会得到合适的格式串。也可以通过直接录入格式串来指定 0 不显示,但格式串必须符合 Grid++Report 的规定格式。另一种实现办法是在报表获取明细记录数据时,在 BeforePostRecord 事件中将值为零的字段设为空,调用字段的 Clear 方法将字段置为空。 问:怎样实现多栏报表? 答:在明细网格上设‘页栏数(PageColumnCount)’属性值大于 1 即可。通过 Grid++Report 的“页栏输出顺序”还可以指定多栏报表的输出顺序是“先从上到下”还是“先从左到右”。 问:如何实现票据套打? 答:Grid++Report 为实现票据套打做了很多专门的安排:报表设计器提供了页面设计模式,按照设定的纸张尺寸显示设计面板,如果将空白票据的扫描图设为设计背景图,在定位报表内容的输出位置会非常方便。报表部件可以设定打印类别,非套打输出的内容在套打打印模式下就不会输出。 问:Grid++Report 有没有横向分页功能? 答:回答是肯定的,在列的总宽度超过打印页面的输出宽度时,Grid++Report 可以另起新页输出剩余的列,如果左边存在锁定列,锁定列可以在后面的新页中重复输出,这样可以保证关键数据列在每一页都有输出。仔细体会 Grid++Report 提供的多种打印适应策略,选用最合适的方式。Grid++Report 的多种打印适应策略为开发动态报表提供了很好的支持。 问:怎样实现报表本页小计功能? 答:定义一个报表分组,将本分组定义为页分组,在本分组的分组头与分组尾上定义统计。页分组就是在每页产生一个分组项,在每页的上端与下端都会分别显示页分组的分组头与分组尾,页分组不用定义分组依据字段。 报表运行 问:怎样与数据库建立连接? 答:如果在设计报表时指定了数据集的数据源连接串与查询 SQL 语句,Grid++Report 采用拉模式直接从数据源取得报表数据,Grid++Report 利用 OLE DB 从数据源取数,OLE DB 提供了广泛的数据源操作能力。如果 Grid++Report 的数据来源采用推模式,即 Grid++Report 不直接与数据库建立连接,各种编程语言/平台都提供了很好的数据库连接方式,并且易于操作,应用程序在报表主对象(IGridppReport)的 FetchRecord 事件中将数据传入,例子程序提供了各种编程语言填入数据的通用方法,对C++Builder 和 Delphi 还进行了专门的包装,直接关联 TDataSet 对象也可以将 TDataSet 对象中的数据传给报表。 问:打印时能否对打印纸张进行自适应?支持表格的折行打印吗? 答:Grid++Report 在打印时采用多种适应策略,通过设置明细网格(IGRDetailGrid)的‘打印策略(PrintAdaptMethod)’属性指定打印策略。(1)丢弃:按设计时列的宽度输出,超出范围的内容不显示。(2)绕行:按设计时列的宽度输出,如果在当前行不能完整输出,则另起新行进行输出。(3)缩放适应:对所有列的输出宽度进行按比例地缩放,使总宽度等于页面的输出宽度。(4)缩小适应:如果列的总宽度小于页面的输出宽度,对所有列的输出宽度进行按比例地缩小,使总宽度等于页面的输出宽度。(5)横向分页:超范围的列在新页中输出。(6)横向分页并重复锁定列。 问:如何改变缺省打印预览窗口的窗口标题? 答:改变报表主对象的‘标题(Title)’属性即可。 问:利用集合对象的编程接口取子对象的接口引用,但不是自己期望的结果。 答:Grid++Report中所有集合对象的下标索引都是从 1 开始,另按对象的名称查找对象的接口引用时,名称字符是不区分大小写的。 问:怎样在运行时控制报表中各个对象的可见性?即怎样在运行时显示或隐藏对象? 答:在报表主对象(GridppReport)的 SectionFormat 事件中设定相应报表子对象的可见(Visible)属性即可。 问:报表主对象重新载入数据,设计器中为什么没有反映新载入的数据? 答:应调用 IGRDesigner 的 Reload 方法。 问:怎样实现不进入打印预览界面,直接将报表打印出来?
-
教你用简易代码实现网站黑白主题的一键切换方法