用prototype實(shí)現(xiàn)的簡單小巧的多級聯(lián)動菜單
更新時(shí)間:2007年03月24日 00:00:00 作者:
使用prototype.js這個(gè)js庫,這個(gè)在網(wǎng)上一搜就能找到了,是一個(gè)開源的js函數(shù)庫。
看到今天貼了幾個(gè)聯(lián)動菜單的帖子
這個(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]);
}
}
}
簡單說一下幾個(gè)參數(shù)吧:
source 第一級菜單
target 聯(lián)動菜單
ignore 當(dāng)有時(shí)候3級聯(lián)動時(shí),例如 國家 省 市 例如上海沒有省的,可以忽略第3級菜單
url action url
options action參數(shù)
excute 是否聯(lián)動
拿比較常見的例子來看 國家 省 市 3級聯(lián)動來作為例子
代碼
<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>', {});
看到今天貼了幾個(gè)聯(lián)動菜單的帖子
這個(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]);
}
}
}
簡單說一下幾個(gè)參數(shù)吧:
source 第一級菜單
target 聯(lián)動菜單
ignore 當(dāng)有時(shí)候3級聯(lián)動時(shí),例如 國家 省 市 例如上海沒有省的,可以忽略第3級菜單
url action url
options action參數(shù)
excute 是否聯(lián)動
拿比較常見的例子來看 國家 省 市 3級聯(lián)動來作為例子
代碼
<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二級聯(lián)動菜單實(shí)現(xiàn)原理及代碼
- 基于jquery的二級聯(lián)動菜單實(shí)現(xiàn)代碼
- 基于asp+ajax和數(shù)據(jù)庫驅(qū)動的二級聯(lián)動菜單
- javascript 無限聯(lián)動菜單效果代碼
- php 三級聯(lián)動菜單
- 一個(gè)強(qiáng)健 實(shí)用的asp+ajax二級聯(lián)動菜單(有演示和附源程序打包下載)
- JS實(shí)多級聯(lián)動下拉菜單類,簡單實(shí)現(xiàn)省市區(qū)聯(lián)動菜單!
- jquery 實(shí)現(xiàn)二級/三級/多級聯(lián)動菜單的思路及代碼
相關(guān)文章
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼
初學(xué)prototype,發(fā)個(gè)JS接受URL參數(shù)的代碼...2006-09-09Prototype1.5 rc2版指南最后一篇之Position
Prototype1.5 rc2版指南最后一篇之Position...2007-01-01Prototype Object對象 學(xué)習(xí)
該不是一個(gè)概念。因?yàn)镃#中的命名空間后面不會直接跟方法,肯定是接一個(gè)對象然后在調(diào)用方法,不過和C++中的命名空間倒是有些類似2009-07-07Prototype 學(xué)習(xí) 工具函數(shù)學(xué)習(xí)($方法)
$方法——被成為瑞士軍刀$方法使用技巧。2009-07-07使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問題.
使用prototype.js 的時(shí)候應(yīng)該特別注意的幾個(gè)問題....2007-04-04滾動經(jīng)典最新話題[prototype框架]下編寫
滾動經(jīng)典最新話題[prototype框架]下編寫...2006-10-10prototype Element學(xué)習(xí)筆記(篇二)
這一篇主要是要總論Element的所有函數(shù)。2008-10-10