|
看了论坛里面关于jsonp 的东西后试着做了一个 但是有些问题- $(function () {
- $.ajax({
- type: "get",
- async: false,
- url: "http://202.196.107.98/wpphp/index.php/admin/index/phone",
- dataType: "jsonp",
- jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
- jsonpCallback:"phone",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写"?",jQuery会自动为你处理数据
-
- success: function(json){
- fourSelectData =json['result'];
- },
- error: function(){
- alert('fail');
- }
- });
- });
复制代码 利用这个可以从数据库取到数据 并且付给了变量fourSelectData 但是我想在这个function下面的代码中使用这个变量fourSelectData- //var fourSelectData = {"金明校区王":{val:"01",items:{"七号教学楼":{val:"0101",items:{"第一层":{val:"010101",items:{"7101":"01010101","7102":"01010102","7103":"01010103",}},"第二层":{val:"010102",items:{}},"第三层":{val:"010103",items:{}},"第四层":{val:"010104",items:{}},"第五层":{val:"010105",items:{}},}},"六号教学楼":{val:"0102",items:{"第三层":{val:"010201",items:{}},}},}},"明伦校区":{val:"02",items:{"10号教学楼":{val:"0201",items:{"1":{val:"020101",items:{}},"2":{val:"020102",items:{}},"3":{val:"020103",items:{}},"4":{val:"020104",items:{}},"5":{val:"020105",items:{}},}},"综合教学楼":{val:"0202",items:{"1":{val:"020201",items:{}},"2":{val:"020202",items:{}},"3":{val:"020203",items:{}},"4":{val:"020204",items:{}},"5":{val:"020205",items:{}},}},}},};
- var defaults = {
- s1:'Select1',
- s2:'Select2',
- s3:'Select3',
- s4:'Select4',
- v1:"02",
- v2:"0202",
- v3:"020202",
- v4:"02020202"
- };
- $(function(){
- fourSelect(defaults);
- });
- function fourSelect(config){
- var $s1=$("#"+config.s1);
- var $s2=$("#"+config.s2);
- var $s3=$("#"+config.s3);
- var $s4=$("#"+config.s4);
- var v1=config.v1?config.v1:null;
- var v2=config.v2?config.v2:null;
- var v3=config.v3?config.v3:null;
- var v4=config.v4?config.v4:null;
- $.each(fourSelectData,function(k,v){
- appendOptionTo($s1,k,v.val,v1);
- });
- $s1.change(function(){
- $s2.html("");
- if(this.selectedIndex==-1) return;
- var s1_curr_val = this.options[this.selectedIndex].value;
- $.each(fourSelectData,function(k,v){
- if(s1_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- appendOptionTo($s2,k,v.val,v2);
- });
- }
- }
- });
- if($s2[0].options.length==0){appendOptionTo($s2,"...","",v2);}
- $s2.change();
- }).change();
- $s2.change(function(){
- $s3.html("");
- if(this.selectedIndex==-1) return;
- var s1_curr_val = $s1[0].options[$s1[0].selectedIndex].value;
- var s2_curr_val = this.options[this.selectedIndex].value;
- $.each(fourSelectData,function(k,v){
- if(s1_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- if(s2_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- appendOptionTo($s3,k,v.val,v3);
- });
- }
- }
- });
- }
- }
- });
- if($s3[0].options.length==0){appendOptionTo($s3,"...","",v3);}
- $s3.change();
- }).change();
- $s3.change(function(){
- $s4.html("");
- if(this.selectedIndex==-1) return;
- var s1_curr_val = $s1[0].options[$s1[0].selectedIndex].value;
- var s2_curr_val = $s2[0].options[$s2[0].selectedIndex].value;
- var s3_curr_val = this.options[this.selectedIndex].value;
- $.each(fourSelectData,function(k,v){
- if(s1_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- if(s2_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- if(s3_curr_val==v.val){
- if(v.items){
- $.each(v.items,function(k,v){
- appendOptionTo($s4,k,v,v4);
- });
- }
- }
- });
- }
- }
- });
- }
- }
- });
- if($s4[0].options.length==0){appendOptionTo($s4,"...","",v4);}
- }).change();
- function appendOptionTo($o,k,v,d){
- var $opt=$("<option>").text(k).val(v);
- if(v==d){ $opt.attr("selected", "selected")}
- $opt.appendTo($o);
- }
- }
复制代码 但是在下面无法获取到变量fourSelectData 因此想问下 利用jsonp取到数据后 我知道怎么让HTML去使用 那怎么让js 使用呢
附件里是我写的源代码 将//var fourSelectData =这个注释取消注释后就能看到想要的四级联动效果 因此想要实现的就是 jsonp取到值赋给fourSelectData
|
|