index.html 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>cxSelect 联动下拉菜单 &raquo; 在线演示 - 前端开发仓库</title>
  6. <style>
  7. body{background:#ddd;font:14px/1.7 tahoma,'\5fae\8f6f\96c5\9ed1',sans-serif;}
  8. h1,h2,h3{font-size:36px;line-height:1;}
  9. h2{font-size:24px;}
  10. h3{font-size:18px;}
  11. fieldset{margin:2em 0;}
  12. fieldset legend{font-weight:bold;font-size:16px;line-height:2;}
  13. select,button{padding:0.5em;}
  14. a{color:#06f;text-decoration:none;}
  15. a:hover{color:#00f;}
  16. .wrap{width:900px;margin:0 auto;padding:20px 50px;border-radius:8px;background:#fff;box-shadow:0 0 10px rgba(0,0,0,0.2);}
  17. </style>
  18. </head>
  19. <body>
  20. <div class="wrap">
  21. <h1>cxSelect 联动下拉菜单</h1>
  22. <h2>示例</h2>
  23. <p>如果无法通过 AJAX 获取数据,请运行在服务器环境下。</p>
  24. <fieldset id="city_china">
  25. <legend>默认</legend>
  26. <p>省份:<select class="province other">
  27. <option>请选择</option>
  28. </select></p>
  29. <p>城市:<select class="city">
  30. <option>请选择</option>
  31. </select></p>
  32. <p>地区:<select class="area">
  33. <option>请选择</option>
  34. </select></p>
  35. </fieldset>
  36. <fieldset id="city_china_val">
  37. <legend>设置默认值及选项标题</legend>
  38. <p>所在地区:
  39. <select class="province other" data-first-title="选择省">
  40. <option value="">请选择</option>
  41. <option value="浙江省" selected>浙江省</option>
  42. </select>
  43. <select class="city" data-first-title="选择市">
  44. <option value="">请选择</option>
  45. <option value="杭州市" selected>杭州市</option>
  46. </select>
  47. <select class="area" data-first-title="选择地区">
  48. <option value="">请选择</option>
  49. <option value="西湖区" selected>西湖区</option>
  50. </select>
  51. </p>
  52. </fieldset>
  53. <!-- <fieldset id="city_china_val">
  54. <legend>多种设置默认值的方式</legend>
  55. <p>方法一:使用 option 的 value 和 selected 属性</p>
  56. <p><select class="province">
  57. <option>浙江省</option>
  58. </select></p>
  59. <p>如果有多个 option ,需要使用 selected 属性指定选中项</p>
  60. <p><select class="city">
  61. <option>请选择</option>
  62. <option value="杭州市" selected>杭州市</option>
  63. </select></p>
  64. <p>方法二:使用 select 的 data-value 属性</p>
  65. <p><select class="area" data-value="西湖区">
  66. <option>请选择</option>
  67. </select></p>
  68. </fieldset> -->
  69. <fieldset id="global_location">
  70. <legend>全球主要国家城市联动</legend>
  71. <p>所在地区:
  72. <select class="country" data-first-title="选择国家"></select>
  73. <select class="state" data-required="true"></select>
  74. <select class="city" data-required="true"></select>
  75. <select class="region" data-required="true"></select>
  76. </p>
  77. </fieldset>
  78. <fieldset id="custom_data">
  79. <legend>自定义选项</legend>
  80. <p>一:<select class="first"></select></p>
  81. <p>二:<select class="second"></select></p>
  82. <p>三:<select class="third"></select></p>
  83. <p>四:<select class="fourth"></select></p>
  84. <p>五:<select class="fifth"></select></p>
  85. </fieldset>
  86. <fieldset>
  87. <legend>API 接口</legend>
  88. <div id="api_data">
  89. <dl>
  90. <dt>选择器组 A</dt>
  91. <dd>
  92. <select class="province select"></select>
  93. <select class="city select"></select>
  94. <select class="area select"></select>
  95. </dd>
  96. </dl>
  97. <dl>
  98. <dt>选择器组 B</dt>
  99. <dd>
  100. <select class="first select"></select>
  101. <select class="second select"></select>
  102. <select class="third select"></select>
  103. <select class="fourth select"></select>
  104. <select class="fifth select"></select>
  105. </dd>
  106. </dl>
  107. </div>
  108. <p>
  109. <button type="button" class="button green" name="attach" value="">attach</button>
  110. <button type="button" class="button green" name="detach" value="">detach</button>
  111. <button type="button" class="button green" name="clear" value="">clear</button>
  112. </p>
  113. <p>
  114. <button type="button" class="button green" name="setSelect" value="b">更换 select 组</button>
  115. <button type="button" class="button green" name="setData" value="china">更换数据:国内数据</button>
  116. <button type="button" class="button green" name="setData" value="global">更换数据:全球数据</button>
  117. <button type="button" class="button green" name="setData" value="custom">更换数据:自定义数据</button>
  118. </p>
  119. <p>
  120. <button type="button" class="button green" name="required" value="0">更改 required</button>
  121. <button type="button" class="button green" name="emptyStyle" value="">更改 emptyStyle</button>
  122. <button type="button" class="button green" name="firstTitle" value="请选择">更改 firstTitle</button>
  123. </p>
  124. </fieldset>
  125. <h2>文档</h2>
  126. <ul>
  127. <li><a target="_blank" href="https://github.com/ciaoca/cxSelect">Github</a></li>
  128. <li><a target="_blank" href="http://code.ciaoca.com/jquery/cxSelect/">中文文档</a></li>
  129. </ul>
  130. <h2>作者</h2>
  131. <p><a target="_blank" href="http://ciaoca.com/">http://ciaoca.com/</a></p>
  132. <p>Released under the MIT license</p>
  133. </div>
  134. <script src="http://cdn.staticfile.org/jquery/1.11.3/jquery.min.js"></script>
  135. <!-- <script src="http://cdn.staticfile.org/zepto/1.0/zepto.min.js"></script> -->
  136. <script src="js/jquery.cxselect.js"></script>
  137. <script>
  138. (function() {
  139. var urlChina = 'js/cityData.min.json';
  140. var urlGlobal = 'js/globalData.min.json';
  141. var dataCustom = [
  142. {'v': '1', 'n': '第一级 >', 's': [
  143. {'v': '2', 'n': '第二级 >', 's': [
  144. {'v': '3', 'n': '第三级 >', 's': [
  145. {'v': '4', 'n': '第四级 >', 's': [
  146. {'v': '5', 'n': '第五级 >', 's': [
  147. {'v': '6', 'n': '第六级 >'}
  148. ]}
  149. ]}
  150. ]}
  151. ]}
  152. ]},
  153. {'v': 'test number', 'n': '测试数字', 's': [
  154. {'v': 'text', 'n': '文本类型', 's': [
  155. {'v': '4', 'n': '4'},
  156. {'v': '5', 'n': '5'},
  157. {'v': '6', 'n': '6'},
  158. {'v': '7', 'n': '7'},
  159. {'v': '8', 'n': '8'},
  160. {'v': '9', 'n': '9'},
  161. {'v': '10', 'n': '10'}
  162. ]},
  163. {'v': 'number', 'n': '数值类型', 's': [
  164. {'v': 11, 'n': 11},
  165. {'v': 12, 'n': 12},
  166. {'v': 13, 'n': 13},
  167. {'v': 14, 'n': 14},
  168. {'v': 15, 'n': 15},
  169. {'v': 16, 'n': 16},
  170. {'v': 17, 'n': 17}
  171. ]}
  172. ]},
  173. {'v': 'test boolean','n': '测试 Boolean 类型', 's': [
  174. {'v': true ,'n': true},
  175. {'v': false ,'n': false}
  176. ]},
  177. {v: 'test quotes', n: '测试属性不加引号', s: [
  178. {v: 'quotes', n: '引号'}
  179. ]},
  180. {v: 'test other', n: '测试奇怪的值', s: [
  181. {v: '[]', n: '数组(空)'},
  182. {v: [1,2,3], n: '数组(数值)'},
  183. {v: ['a','b','c'], n: '数组(文字)'},
  184. {v: new Date(), n: '日期'},
  185. {v: new RegExp('\\d+'), n: '正则对象'},
  186. {v: /\d+/, n: '正则直接量'},
  187. {v: {}, n: '对象'},
  188. {v: document.getElementById('custom_data'), n: 'DOM'},
  189. {v: null, n: 'Null'},
  190. {n: '未设置 value'}
  191. ]},
  192. {'v': '' , 'n': '无子级'}
  193. ];
  194. $.cxSelect.defaults.url = urlChina;
  195. // 默认
  196. $('#city_china').cxSelect({
  197. selects: ['province', 'city', 'area']
  198. });
  199. // 设置默认值及选项标题
  200. $('#city_china_val').cxSelect({
  201. selects: ['province', 'city', 'area'],
  202. emptyStyle: 'none'
  203. });
  204. // 全球主要国家城市联动
  205. $('#global_location').cxSelect({
  206. url: urlGlobal,
  207. selects: ['country', 'state', 'city', 'region'],
  208. emptyStyle: 'none'
  209. });
  210. // 自定义选项
  211. $('#custom_data').cxSelect({
  212. selects: ['first', 'second', 'third', 'fourth', 'fifth'],
  213. // required: true,
  214. jsonValue: 'v',
  215. data: dataCustom
  216. });
  217. // API 接口
  218. var apiBox = $('#api_data');
  219. var cxSelectApi;
  220. apiBox.cxSelect({
  221. selects: ['province', 'city', 'area']
  222. }, function(api) {
  223. cxSelectApi = api;
  224. });
  225. // cxSelectApi = $.cxSelect(apiBox, {
  226. // selects: ['province', 'city', 'area']
  227. // });
  228. $('body').on('click', 'button', function() {
  229. var _name = this.name;
  230. var _value = this.value;
  231. switch (_name) {
  232. case 'attach':
  233. cxSelectApi.attach();
  234. break;
  235. case 'detach':
  236. cxSelectApi.detach();
  237. break;
  238. case 'clear':
  239. cxSelectApi.clear();
  240. break;
  241. case 'required':
  242. cxSelectApi.setOptions({
  243. required: _value == 1 ? false : true
  244. });
  245. this.value = _value == 1 ? 0 : 1;
  246. break;
  247. case 'emptyStyle':
  248. if (_value === 'none') {
  249. _value = 'hidden';
  250. } else if (_value === 'hidden') {
  251. _value = '';
  252. } else {
  253. _value = 'none';
  254. };
  255. cxSelectApi.setOptions({
  256. emptyStyle: _value
  257. });
  258. this.value = _value;
  259. break;
  260. case 'firstTitle':
  261. _value = _value === '请选择' ? '选择吧' : '请选择';
  262. cxSelectApi.setOptions({
  263. firstTitle: _value
  264. });
  265. this.value = _value;
  266. break;
  267. case 'setSelect':
  268. cxSelectApi.setOptions({
  269. selects: _value === 'a' ? ['province', 'city', 'area'] : ['first', 'second', 'third', 'fourth', 'fifth']
  270. });
  271. this.value = _value === 'a' ? 'b' : 'a';
  272. break;
  273. case 'setData':
  274. if (_value === 'china' || _value === 'global') {
  275. // $.ajax({
  276. // url: this.value === 'china' ? urlChina : urlGlobal,
  277. // type: 'GET',
  278. // dataType: 'json'
  279. // }).done(function(data, textStatus, jqXHR) {
  280. cxSelectApi.setOptions({
  281. url: this.value === 'china' ? urlChina : urlGlobal,
  282. // data: data
  283. });
  284. // }).fail(function(jqXHR, textStatus, errorThrown) {
  285. // });
  286. } else if (this.value === 'custom') {
  287. cxSelectApi.setOptions({
  288. data: dataCustom
  289. });
  290. };
  291. break;
  292. // not default
  293. };
  294. });
  295. })();
  296. </script>
  297. </body>
  298. </html>