Javascript級聯下拉式功能表以及AJAX資料驗證核心代碼

來源:互聯網
上載者:User

雖然也使用了Prototype.js來編寫,但是由於對它的不瞭解,類的實現仍然是使用了《JavaScript進階程式設計》裡的方法。使用AJAX進行資料驗證時,最初使用的是XML來當資料來源,然而在使用了一段時間後,發現XML效率太低,於是又使用JSON來做為資料來源。

一年過去了,客戶又提出了新的需求,最初是只要輸入框的兩個資料相符就行,現在的要求是兩個下拉式功能表的資料也要相符,於是,我利用此機會,將代碼重構了一次。

需求
1、根據下拉式功能表產品名稱、產品封裝的選擇,右面的圖片要進行相應的變化。
2、產品名稱、產品封裝、生產日期、生產批次都驗證正確後,右圖出現相應的提示。

簡要說明
使用Prototyp.js完成類的構建,物件導向的方式實現功能,事件導向讓代碼更清晰明了,使用AJAX的狀態管理,讓驗證過程對使用者更友好,JSON作為資料來源的執行效率也讓人滿意。
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: 複製代碼 代碼如下:var ValidProduct = Class.create();
ValidProduct.prototype = {
initialize:function(prodData,validDataUrl,validData,prodType,prodPack,prodDate,prodPatch,prodImg,validBtn,validMsg){
this.prodData = $H(prodData); //產品類別資料 | product type data
this.validDataUrl = validDataUrl; //驗證資料路徑 | product data url
this.validData = validData; //驗證資料 | product data
this.prodType = $(prodType); //產品驗證類別 | product type
this.prodPack = $(prodPack); //產品驗證封裝裝 | product package
this.prodDate = prodDate; //產品驗證日期ID | product date
this.prodPatch = prodPatch; //產品驗證批次ID | product batch
this.prodImg = $(prodImg); //產品驗證圖片 | product images
this.validBtn = $(validBtn); //產品驗證按鈕 | validate button
this.validMsg = $(validMsg); //產品驗證過程提示 | 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(){//綁定產品類別下拉式清單資料 | 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){//產品類別下拉式清單事件監聽 | 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){//綁定產品封裝下拉式清單資料 | 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){//產品封裝下拉式清單事件監聽 | Eventlistener of product package
var o = this.prodPack;
this.prodImg.writeAttribute("src",o[o.selectedIndex].id);
},
productValid:function(){//產品驗證 | 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("請選擇產品及封裝!");
}
}else{
alert("請填好產品生產日期和產品批號!");
}
},
_validProd:function(oReq){//產品驗證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");
});

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.