首页 > web前端 > js教程 > 正文

用prototype实现的简单小巧的多级联动菜单

PHP中文网
发布: 2016-05-16 19:16:12
原创
975人浏览过

使用prototype.js这个js库,这个在网上一搜就能找到了,是一个开源的js函数库。 
看到今天贴了几个联动菜单的帖子
这个应该大家都有各自比较好的代码了
我也顺手贴一个我们team里面用的比较小巧的代码

// author: downpour   
var doublecombo = class.create();   
doublecombo.prototype = {   
  initialize: function(source, target, ignore, url, options, excute) {   
    this.source = $(source);   
    this.target = $(target);   
    this.ignore = $a(ignore);   
    this.url = url;   
    this.options = $h(options);   
    this.source.onchange = this.dochange.bindaseventlistener(this);   
    if(excute) {   
        this.dochange();   
    }   
  },   
  dochange: function() {   
    if(this.source.value != '') {   
        // first clear the ignore ones   
        this.ignore.each(   
            function(value) {   
                $(value).options.length = 1;   
                $(value).options[0].selected = 'selected';   
            }   
        );   
        // create parameter for ajax   
        var query = $h({ id: this.source.value });   
        var parameters = {   
            method: 'post',    
            parameters: $h(this.options).merge(query).toquerystring(),    
            oncomplete: this.getresponse.bindaseventlistener(this)   
        }   
        var locationrequest = new ajax.request( this.url, parameters );   
    }   
  },   
  getresponse: function(request) {   
    this.target.options.length = 1;   
    this.target.options[0].selected = 'selected';   
    var response = $a(request.responsetext.trim().split(';'));   
    response.length--;   
    for(var i = 0; i         var optionparam = response[i].split(',');   
        this.target.options[this.target.options.length] = new option(optionparam[1], optionparam[0]);   
    }   
  }   
}  
简单说一下几个参数吧: 
source 第一级菜单
target 联动菜单
ignore 当有时候3级联动时,例如 国家 省 市 例如上海没有省的,可以忽略第3级菜单
url action url
options action参数
excute 是否联动
拿比较常见的例子来看 国家 省 市 3级联动来作为例子
代码
  
  
  
<script> <br/> new doublecombo(&#39;country&#39;, &#39;province&#39;, null, &#39;<c:url value="/xxxx.do?combo=true">&#39;, {}); <br/><script type="text/javascript"> <br/> new doublecombo(&#39;province&#39;, &#39;city&#39;, null, &#39;<c:url value="/xxxx.do?combo=true">&#39;, {}); </script>

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号