博客
关于我
给select赋值过程
阅读量:400 次
发布时间:2019-03-05

本文共 1563 字,大约阅读时间需要 5 分钟。

初始化省份选项

通过set_province()函数初始化省份选项:

function set_province() { for (var i in region) { province[i - 1] = new Option(region[i].name, region[i].name); }}

基于事件的初始化

在页面加载完成后,通过jQuery.each遍历所有select元素:

$(document).ready(function () { $("select").each(function () { var $ele = $(this); var str = "@Model.personalDatas.personalDataAddress"; var $value = str.split("|"); $ele.find("option[value='" + $value[0] + "']").attr("selected", "selected"); var index = $ele.find("option[value='" + $value[0] + "']").index(); var data_region = region[index + 1]; city.length = 1; for (var i in data_region['child']) { city[i] = new Option(data_region['child'][i].name, data_region['child'][i].name); } $ele.find("option[value='" + $value[1] + "']").attr("selected", "selected"); console.log(province.selectedIndex); var data_region1 = region[province.selectedIndex + 1]['child'][city.selectedIndex]['child']; zone.length = 1; for (var i in data_region1) { for (var j in data_region1[i]) { zone[j] = new Option(data_region1[i][j].name, data_region1[i][j].name); } } $ele.find("option[value='" + $value[2] + "']").attr("selected", "selected"); $("#address").val($value[3]); });});

功能说明

该代码主要完成以下功能:

  • 初始化省份选项
  • 根据用户选择的省份自动加载区域信息
  • 基于区域信息初始化城市和区的选项
  • 设置默认地址信息

转载地址:http://gitzz.baihongyu.com/

你可能感兴趣的文章
npm ERR! fatal: unable to connect to github.com:
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>