Home > Web Front-end > H5 Tutorial > HTML5 option attribute: How to use the option attribute to implement a cascading drop-down list

HTML5 option attribute: How to use the option attribute to implement a cascading drop-down list

不言
Release: 2018-08-09 14:45:08
Original
4417 people have browsed it

This article introduces you to the option attribute of HTML5: how to use the option attribute to implement a cascading drop-down list. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

<!DOCTYPE>
<html>
<head>
<title>级联下拉列表</title>
<meta charset="UTF-8">
</head>
<body onload="load()">
<p>
<select class=&#39;prov&#39; id=&#39;prov&#39; onchange=&#39;changeCity()&#39;>
<option value=&#39;&#39;>--请选择省--</option>
</select>
<select class=&#39;city&#39; id=&#39;city&#39;>
<option value=&#39;&#39;>--请选择市--</option>
</select>
</p>
<script>
var province=document.getElementById("prov");
var city=document.getElementById("city");
var arr_prov=new Array(new Option("--请选择省--",&#39;&#39;),new Option("湖南","hn"),new Option("广东","gd"));
var arr_city=new Array();
arr_city[0]=new Array(new Option("--请选择市--",&#39;&#39;));
arr_city[1]=new Array(new Option("长沙",&#39;cs&#39;),new Option("娄底",&#39;ld&#39;),new Option("永州",&#39;yz&#39;));
arr_city[2]=new Array(new Option("广州",&#39;gz&#39;),new Option("深圳",&#39;sz&#39;)); //动态载入所有省份
function load(){ for(var i=0;i<arr_prov.length;i++){ province.options[i]=arr_prov[i]; } } //选中省份之后,根据索引动态载入相应城市 function changeCity(){
//清空上次的选项
city.options.length=0; //获取省一级的下拉列表选中的索引
var index=province.selectedIndex;
for(var i=0;i<arr_city[index].length;i++){
city.options[i]=arr_city[index][i];
} }
</script>
</body>
</html>
Copy after login

Recommended related articles:

html5 Custom attributes: How to get custom attribute values ​​(with code)

How the Select object of HTML performs operations on the Option object

The above is the detailed content of HTML5 option attribute: How to use the option attribute to implement a cascading drop-down list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template