用prototype實(shí)現(xiàn)的簡(jiǎn)單小巧的多級(jí)聯(lián)動(dòng)菜單
看到今天貼了幾個(gè)聯(lián)動(dòng)菜單的帖子
這個(gè)應(yīng)該大家都有各自比較好的代碼了
我也順手貼一個(gè)我們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 < response.length; i++) {
var optionParam = response[i].split(',');
this.target.options[this.target.options.length] = new Option(optionParam[1], optionParam[0]);
}
}
}
簡(jiǎn)單說(shuō)一下幾個(gè)參數(shù)吧:
source 第一級(jí)菜單
target 聯(lián)動(dòng)菜單
ignore 當(dāng)有時(shí)候3級(jí)聯(lián)動(dòng)時(shí),例如 國(guó)家 省 市 例如上海沒(méi)有省的,可以忽略第3級(jí)菜單
url action url
options action參數(shù)
excute 是否聯(lián)動(dòng)
拿比較常見(jiàn)的例子來(lái)看 國(guó)家 省 市 3級(jí)聯(lián)動(dòng)來(lái)作為例子
代碼
<html-el:select property="country" styleId="country" >
<html-el:options collection="countries" property="id" labelProperty="name" />
</html-el:select>
<html-el:select property="province" styleId="province">
<option value="">--Please Select--</option>
................
</html-el:select>
<html-el:select property="city" styleId="city">
<option value="">--Please Select--</option>
................
</html-el:select>
<script type="text/javascript">
new DoubleCombo('country', 'province', null, '<c:url value="/xxxx.do?combo=true"></c:url>', {});
<script type="text/javascript">
new DoubleCombo('province', 'city', null, '<c:url value="/xxxx.do?combo=true"></c:url>', {});
- Ajax二級(jí)聯(lián)動(dòng)菜單實(shí)現(xiàn)原理及代碼
- 基于jquery的二級(jí)聯(lián)動(dòng)菜單實(shí)現(xiàn)代碼
- 基于asp+ajax和數(shù)據(jù)庫(kù)驅(qū)動(dòng)的二級(jí)聯(lián)動(dòng)菜單
- javascript 無(wú)限聯(lián)動(dòng)菜單效果代碼
- php 三級(jí)聯(lián)動(dòng)菜單
- 一個(gè)強(qiáng)健 實(shí)用的asp+ajax二級(jí)聯(lián)動(dòng)菜單(有演示和附源程序打包下載)
- JS實(shí)多級(jí)聯(lián)動(dòng)下拉菜單類,簡(jiǎn)單實(shí)現(xiàn)省市區(qū)聯(lián)動(dòng)菜單!
- jquery 實(shí)現(xiàn)二級(jí)/三級(jí)/多級(jí)聯(lián)動(dòng)菜單的思路及代碼
相關(guān)文章
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼...2006-09-09
Prototype1.5 rc2版指南最后一篇之Position
Prototype1.5 rc2版指南最后一篇之Position...2007-01-01
Prototype Array對(duì)象 學(xué)習(xí)
這個(gè)對(duì)象擴(kuò)展了JS原生的Array對(duì)象,提供了一些基本的工具函數(shù),有些方法非常簡(jiǎn)單,源碼里就不在注釋了。2009-07-07
Prototype Object對(duì)象 學(xué)習(xí)
該不是一個(gè)概念。因?yàn)镃#中的命名空間后面不會(huì)直接跟方法,肯定是接一個(gè)對(duì)象然后在調(diào)用方法,不過(guò)和C++中的命名空間倒是有些類似2009-07-07
Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($方法)
$方法——被成為瑞士軍刀$方法使用技巧。2009-07-07
使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問(wèn)題.
使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問(wèn)題....2007-04-04
滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫(xiě)
滾動(dòng)經(jīng)典最新話題[prototype框架]下編寫(xiě)...2006-10-10
prototype Element學(xué)習(xí)筆記(篇二)
這一篇主要是要總論Element的所有函數(shù)。2008-10-10

