亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Javascript級(jí)聯(lián)下拉菜單以及AJAX數(shù)據(jù)驗(yàn)證核心代碼

 更新時(shí)間:2013年05月10日 15:28:19   作者:  
最初是只要輸入框的兩個(gè)數(shù)據(jù)相符就行,現(xiàn)在的要求是兩個(gè)下拉菜單的數(shù)據(jù)也要相符,于是,我利用此機(jī)會(huì),將代碼重構(gòu)了一次,感興趣的朋友們可以借此學(xué)習(xí)一下
雖然也使用了Prototype.js來編寫,但是由于對(duì)它的不了解,類的實(shí)現(xiàn)仍然是使用了《JavaScript高級(jí)程序設(shè)計(jì)》里的方法。使用AJAX進(jìn)行數(shù)據(jù)驗(yàn)證時(shí),最初使用的是XML來當(dāng)數(shù)據(jù)源,然而在使用了一段時(shí)間后,發(fā)現(xiàn)XML效率太低,于是又使用JSON來做為數(shù)據(jù)源。

一年過去了,客戶又提出了新的需求,最初是只要輸入框的兩個(gè)數(shù)據(jù)相符就行,現(xiàn)在的要求是兩個(gè)下拉菜單的數(shù)據(jù)也要相符,于是,我利用此機(jī)會(huì),將代碼重構(gòu)了一次。

需求
1、根據(jù)下拉菜單產(chǎn)品名稱、產(chǎn)品包裝的選擇,右面的圖片要進(jìn)行相應(yīng)的變化。
2、產(chǎn)品名稱、產(chǎn)品包裝、生產(chǎn)日期、生產(chǎn)批次都驗(yàn)證正確后,右圖出現(xiàn)相應(yīng)的提示。

簡(jiǎn)要說明
使用Prototyp.js完成類的構(gòu)建,面向?qū)ο蟮姆绞綄?shí)現(xiàn)功能,事件導(dǎo)向讓代碼更清晰明了,使用AJAX的狀態(tài)管理,讓驗(yàn)證過程對(duì)用戶更友好,JSON作為數(shù)據(jù)源的執(zhí)行效率也讓人滿意。
Linkage Drop Down List And AJAX Validation
This JS script has special meaning for me.
I got a new start one year ago, the first task was to solve this linkage drop down list and data validation. At that time I had no deep understanding of Javascript. With my ability of study, after the reference to the code of colleague's, I finally finished it in several days.
Although I used Prototype.js to code, I still used the method in the to make up Class. In the process of AJAX validation, I used XML as data source at the beginning. Aftet time past, I changed data source from XML to JSON for the low efficiency of XML.
Now the clients have new requirements, they need four data to be validated. So I rebuild the scripts.
Requirements:
1. change images of products with the change of product name and package.
2. after the validation with product name, package, date, batch, change images of products.
Brief:
Construct class with Prototype.js, use OOP's approach and event management to make a clear idea.
The management of AJAX status let the process be more friendly for customer. I'm also satisfied with the efficiency of JSON.
核心代碼 | Core Code:
復(fù)制代碼 代碼如下:

var ValidProduct = Class.create();
ValidProduct.prototype = {
initialize:function(prodData,validDataUrl,validData,prodType,prodPack,prodDate,prodPatch,prodImg,validBtn,validMsg){
this.prodData = $H(prodData); //產(chǎn)品類別數(shù)據(jù) | product type data
this.validDataUrl = validDataUrl; //驗(yàn)證數(shù)據(jù)路徑 | product data url
this.validData = validData; //驗(yàn)證數(shù)據(jù) | product data
this.prodType = $(prodType); //產(chǎn)品驗(yàn)證類別 | product type
this.prodPack = $(prodPack); //產(chǎn)品驗(yàn)證包裝 | product package
this.prodDate = prodDate; //產(chǎn)品驗(yàn)證日期ID | product date
this.prodPatch = prodPatch; //產(chǎn)品驗(yàn)證批次ID | product batch
this.prodImg = $(prodImg); //產(chǎn)品驗(yàn)證圖片 | product images
this.validBtn = $(validBtn); //產(chǎn)品驗(yàn)證按鈕 | validate button
this.validMsg = $(validMsg); //產(chǎn)品驗(yàn)證過程提示 | validate message
this.init();
},
init:function(){//程序初始化 | Application init
this.productTypeBind();
this.prodType.observe("change",this.productTypeChange.bind(this));
this.prodPack.observe("change",this.productPackChange.bind(this));
this.validBtn.observe("click",this.productValid.bind(this));
},
productTypeBind:function(){//綁定產(chǎn)品類別下拉列表數(shù)據(jù) | Binding product type data
this.prodPack.selectedIndex = 0; //for IE after page refreshed
var o = this.prodType;
this.prodData.each(function(pair){
o.options.add(new Option(pair.key, pair.value.code));
});
},
productTypeChange:function(e){//產(chǎn)品類別下拉列表事件監(jiān)聽 | Eventlistener of product type
var o = this.prodPack;
o.length = 1;
o.selectedIndex = 0; //for IE after packing choosed the first
this.prodImg.writeAttribute("src",o[0].id);
var selected = this.prodType.selectedIndex;
if (selected!=0){
this.productPackBind(this.prodType[selected].text);
}
},
productPackBind:function(choosedValue){//綁定產(chǎn)品包裝下拉列表數(shù)據(jù) | Binding product package data
var o = this.prodPack;
$H(this.prodData.get(choosedValue).type).each(function(pair){
var newOption = new Option(pair.key, pair.value.packing);
newOption.id = pair.value.img;
o.options.add(newOption);
});
},
productPackChange:function(e){//產(chǎn)品包裝下拉列表事件監(jiān)聽 | Eventlistener of product package
var o = this.prodPack;
this.prodImg.writeAttribute("src",o[o.selectedIndex].id);
},
productValid:function(){//產(chǎn)品驗(yàn)證 | validate product
var v1 = $F(this.prodDate).strip(), v2 = $F(this.prodPatch).strip();
if(v1!=""&&v2!=""){
if(this.prodPack.selectedIndex != 0){
var validAjax = new Ajax.Request(this.validDataUrl,{
method:"get",
parameters:"rnd="+Math.random(),
onCreate: function(){
this.validMsg.show();
}.bind(this),
onComplete:this._validProd.bind(this)
});
}else{
alert("請(qǐng)選擇產(chǎn)品及包裝!");
}
}else{
alert("請(qǐng)?zhí)詈卯a(chǎn)品生產(chǎn)日期和產(chǎn)品批號(hào)!");
}
},
_validProd:function(oReq){//產(chǎn)品驗(yàn)證Ajax callback
this.validMsg.hide();
var v1 = this.prodType.getValue(), v2 = this.prodPack.getValue();
var v3 = $F(this.prodDate).strip(), v4 = $F(this.prodPatch).strip();
var imgUrl = this.prodPack[this.prodPack.selectedIndex].id;
//alert(v1+"n"+v2+"n"+v3+"n"+v4+"n"+imgUrl);
var prodBatchs = oreq.responseText.evalJSON()[this.validData];
var result=prodBatchs.any(function(a){
return (v3==a[1] && v4==a[0] && a[2].startsWith(v1) && v2==a[3]);
});
if(result){
this.prodImg.writeAttribute("src", imgUrl.split(".")[0] + "-valid.jpg");
}else{
this.prodImg.writeAttribute("src", "images/invalid.jpg");
};
}
}
document.observe("dom:loaded",function(){
var validOne = new ValidProduct(prodTypeData,"data/batchs_new2.txt","batchs","productType",
"productPack","prodate","probatch","credit-img","vaSubmit","ajaxsearch");
});

相關(guān)文章

最新評(píng)論