select 的jquery 菜单联动

修改自
* 作者:天天无用
* http://blog.csdn.net/luq885/archive/2007/07/31/1719538.aspx
* blog: http://blog.csdn.net/luq885

  1. var text;
  2. var value;
  3. var type;
  4. var selected;
  5. var keep;
  6. jQuery.fn.FillOptions = function(url,options,endfn){
  7.     if(url.length == 0) throw "request is required";        
  8.     text = options.textfield || "text";
  9.     value = options.valuefiled || "value";    
  10.     type = options.datatype.toLowerCase() || "json";
  11.     if(type != "xml")type="json";
  12.     keep = options.keepold?true:false;
  13.     selected = options.selectedindex || 0;
  14.     $.ajaxSetup({async:false});
  15.     var datas;
  16.     if(type == "xml")
  17.     {
  18.         $.get(url,function(xml){datas=xml;});            
  19.     }
  20.     else
  21.     {
  22.         $.getJSON(url,function(json){datas=json;});
  23.     }
  24.     if(datas == undefined)
  25.     {
  26.         alert("no datas");
  27.         return;
  28.     }
  29.     this.each(function(){
  30.         if(this.tagName == "SELECT")
  31.         {
  32.             var select = this;
  33.             if(!keep)$(select).html("");
  34.             addOptions(select,datas);
  35.         }
  36.     });
  37.     /*给FillOptions 也附加了 endfn 参数 */
  38.     if(typeof endfn =="function") endfn();
  39.     /*解决ie浏览器的刷新 selected的不起作用 http://support.microsoft.com/kb/185123/zh-cn */
  40.     if($.browser.msie && selected) {
  41.         $('option[value='+selected+']',this).attr('selected',true);
  42.     }
  43. }
  44. function addOptions(select,datas)
  45. {        
  46.     var options;
  47.     var datas;
  48.     if(type == "xml")
  49.     {
  50.         $(text,datas).each(function(i){            
  51.             option = new Option($(this).text(),$($(value,datas)[i]).text());
  52.             if($($(value,datas)[i]).text()==selected)option.selected=true;
  53.             select.options.add(option);
  54.         });
  55.     }
  56.     else
  57.     {
  58.         $.each(datas,function(i,n){
  59.             option = new Option(eval("n."+text),eval("n."+value));
  60.             if(eval("n."+value)==selected) {
  61.                 option.selected='selected';
  62.             }
  63.             select.options.add(option);
  64.         });
  65.     }
  66.     
  67. }
  68. jQuery.fn.CascadingSelect = function(target,url,options,endfn){
  69.     $.ajaxSetup({async:false});        
  70.     if(target[0].tagName != "SELECT"throw "target must be SELECT";
  71.     if(url.length == 0) throw "request is required";            
  72.     if(options.parameter == undefined) throw "parameter is required";   
  73.     this.change(function(){
  74.         var newurl = "";
  75.         urlstr = url.split("?");
  76.         newurl = urlstr[0] + "?" + options.parameter + "=" + $(this).val() + "&" +urlstr[1];
  77.         target.FillOptions(newurl,options,endfn);
  78.     });
  79.     /*可以联动二级三级的select*/
  80.     if(options.selectedindex) {
  81.         $(this).trigger('change');
  82.     }   
  83. }
  84. jQuery.fn.AddOption = function(text,value,selected,index){
  85.     option = new Option(text,value);            
  86.     this[0].options.add(option,index);
  87.     this[0].options[index].selected = selected;
  88. }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值