json文件_ajax

时间:2021-02-21 04:37:01

html源码代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ajax json jquery 菜单案例</title>
<style type="text/css">
*{margin:;padding:;}
body { font-size: 13px; line-height: %; padding: 60px }
</style>
<link rel="stylesheet" type="text/css" href="../css/style.css"/>
<script src="../js/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
$(function(){
$("#first").change(function() {
var id=document.getElementById("first").value;
$.ajax({
type: "POST",
url: "../data/menu.json",
dataType: "json",
success: function(data) {
var secondRoot=document.getElementById("second");
secondRoot.innerHTML=null;
for(var i=;i<data.length;i++)
{
if(data[i].cid==id)
{
for( var j=;j<data[i].cname.length;j++)
{
var op = document.createElement("option");
op.innerHTML=(data[i].cname)[j];
secondRoot.appendChild(op);
}
}
}
},
error:function(data){
alert(data);
}
});
});
}); </script>
</head>
<body>
<select name="first" id="first" style="width:160px">
<option value="">---请选择---</option>
<option value="" >中国</option>
<option value="">美国</option>
<option value="">英国</option>
</select>
<select name="second" id="second" size="" style="width:160px"></select>
</body>
</html>

json源代码

[
{
"cid": "",
"cname": [
"子菜单1",
"子菜单1",
"子菜单1"
]
},
{
"cid": "",
"cname": [
"子菜单2",
"子菜单2",
"子菜单2"
]
},
{
"cid": "",
"cname": [
"子菜单3",
"子菜单3",
"子菜单3"
]
} ]