如何用jquery+json來寫頁面

來源:互聯網
上載者:User

標籤:height   getjson   script   body   index   資料表   type   this   如何   

以下是json資料表:

[
{
"p" : "銀川市",
"c" : [{"c1":"興慶區"},{"c1":"金鳳區"},{"c1":"西夏區"},{"c1":"中寧縣"},{"c1":"靈武市"},{"c1":"賀蘭縣"}]
},
{
"p" : "石嘴山市",
"c" : [{"c1":"大武口區"},{"c1":"惠農區"},{"c1":"平羅縣"}]
},
{
"p" : "吳忠市",
"c" : [{"c1":"利通區"},{"c1":"紅寺堡區"},{"c1":"鹽池縣"},{"c1":"同心縣"},{"c1":"青銅峽市"}]
},
{
"p" : "中衛市",
"c" : [{"c1":"沙坡頭區"},{"c1":"中寧縣"},{"c1":"海原縣"}]
},
{
"p" : "固原市",
"c" : [{"c1":"原州區"},{"c1":"涇源縣"},{"c1":"西吉縣"},{"c1":"隆德縣"},{"c1":"彭陽縣"}]
}
]

html代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
<title>城市級聯</title>
</head>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#city,#country{
display: none;
}
#city div{
background-color: #666666;
color: #fff;
width: 30.3%;
margin:1.5% ;
float: left;
}
#country div{
background-color: #666666;
color: #fff;
width: 30.3%;
margin:1.5% ;
float: left;
}
#sheng{
display: none;
}
.cityclass{
background-color: #CCCCCC;
z-index: 100;
}
</style>
<body>
<input type="" name="dest" id="dest" value="" />
<div id="city">

</div>
<div id="country">

</div>
<script>
$(function(){
$(‘#city‘).width($(window).width());
$(‘#city‘).height($(window).height());
$(‘#city‘).addClass(‘cityclass‘);
$("#country").width($(window).width());
$(‘#country‘).height($(window).height());
$(‘#country‘).addClass(‘cityclass‘);
$(‘#dest‘).focus(function(){
$.getJSON("js/dest.json",function(data){
var ss=data;
var html="<p>熱門城市<p>";
for(var i=0;i<ss.length;i++){
html+=‘<div>‘+ss[i].p+‘</div>‘;
}
$("#city").html(html);
$("#city").show();
$(‘#dest‘).hide();
$(‘#city‘).find(‘div‘).click(function(){
var htmls="<p>熱門省市</p>";
for(var i=0;i<ss.length;i++){
var b=$(this).html();
if($(this).html()==ss[i].p){
var a=ss[i].c;
for(var j=0;j<a.length;j++){
htmls+="<div>"+a[j].c1+"</div>";
}
$("#city").hide();
$("#country").html(htmls);
$(‘#country‘).show();
}
$(‘#country‘).find(‘div‘).click(function(){
$(‘#dest‘).val(b+$(this).html());
$(‘#country‘).hide();
$(‘#dest‘).show();
})
}
});
});
})

})

</script>
</body>
</html>

 

如何用jquery+json來寫頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.