You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

66 lines
2.6 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>二级级联查询</title>
</head>
<body>
<select id="first" onChange="nextChange()">
<option selected="selected"></option>
<option value="zhanJiang">湛江</option>
<option>深圳</option>
</select>
<select id="second">
<option></option>
</select>
<!--如果想要使用jq方法请加入下面这个,导入jq的包,这jquery-2.1.1.min.js大家可以去搜索下载,src代表那个相对地址-->
<script type="text/javascript" src="jquery/jquery-2.1.1.min.js"></script>
<script>
function nextChange()
{
var first = document.getElementById("first");
var second = document.getElementById("second");
second.options.length = 0; // 清除second下拉框的所有内容
if(first.selectedIndex == 1)
{
second.options.add(new Option("廉江市", "0"));
second.options.add(new Option("吴川市", "2"));
second.options.add(new Option("坡头区", "1", false, true)); // 默认选中区
}
if(first.selectedIndex == 2)
{
second.options.add(new Option("福田区", "0"));
second.options.add(new Option("罗湖区", "1", false, true)); // 默认选中区
second.options.add(new Option("南山区", "2"));
}
//1、这里是使用js获取下拉框的值方法
/* var object = document.getElementById("first");//获取对应的select对象
var selectedIndex = object.selectedIndex;//获取被选中的下拉框对应的对象索引
var val = object.options[selectedIndex].value;//获取被选中对象的值,注意这里是options,获取的是对象框内的value代表的内容
var text = object.options[selectedIndex].text;//获取被选中对象的text,注意这里是options,获取的是文本内容
alert("object:"+object);
alert("selectedIndex:"+selectedIndex);
alert("value:"+val);
alert("text:"+html);*/
//2.这里是使用jq获取下拉框的值方法
var selectedFirst=$("#first option:selected"); //获取第一个下拉框的被选中对象的值
var selectedFirstVal = selectedFirst.val(); //拿到选中项的值,比如选中湛江,获取的值为“zhanJiang”;
var selectedFirstText = selectedFirst.text(); //拿到选中项的文本,比如选中湛江,获取的值为“湛江”
alert("selectedFirst:"+selectedFirst);
alert("selectedFirstVal:"+selectedFirstVal);
alert("selectedFirstText:"+selectedFirstText);
}
</script>
</body>
</html>