嘗試在添加到購物車按鈕上爲我的wordpress網站上我的simplecart貨架項目上的大小和顏色的下拉選擇設置一些驗證。我可以彈出警報並阻止添加項目,但這是錯誤的,因爲即使選擇了適當的值,警報也會彈出。我一直在尋找這幾天,並發現有幾個人問同樣的問題沒有迴應,所以任何關於如何和如果這是可能的投入將是驚人的。simpleCart(js)選擇選項驗證
這裏是添加到購物車功能:
me.add = function() {
var me=this;
/* load cart values if not already loaded */
if(!me.pageIsReady ) {
me.initializeView();
me.update();
}
if(!me.isLoaded ) {
me.load();
me.update();
}
var newItem = new CartItem();
/* check to ensure arguments have been passed in */
if(!arguments || arguments.length === 0){
error('No values passed for item.');
return;
}
var argumentArray = arguments;
if(arguments[0] && typeof(arguments[0]) != 'string' && typeof(arguments[0]) != 'number' ){
argumentArray = arguments[0];
}
newItem.parseValuesFromArray(argumentArray);
newItem.checkQuantityAndPrice();
的代碼我想實現:
if(item_Size = "none"){
alert("Please select a size");
return;
}
if(item_Color = "none"){
alert("Please select a color");
return;
}
現在我是個菜鳥,當談到這個東西,所以林甚至不知道在哪裏它存儲值,因此它可以是任何設置變量名稱,並且代碼可能完全錯誤,但沒有一個是默認選擇選項的值。
這是怎麼回事加載和保存的cookies:
me.load = function() {
var me = this;
/* initialize variables and items array */
me.items = {};
me.total = 0.00;
me.quantity = 0;
/* retrieve item data from cookie */
if(readCookie('simpleCart')){
var data = unescape(readCookie('simpleCart')).split('++');
for(var x=0, xlen=data.length;x<xlen;x++){
var info = data[x].split('||');
var newItem = new CartItem();
if(newItem.parseValuesFromArray(info)){
newItem.checkQuantityAndPrice();
/* store the new item in the cart */
me.items[newItem.id] = newItem;
}
}
}
me.isLoaded = true;
};
/* save cart to cookie */
me.save = function() {
var dataString = "";
for(var item in this.items){
dataString = dataString + "++" + this.items[item].print();
}
createCookie('simpleCart', dataString.substring(2), 30);
};
完整simpleCart代碼可以在這裏找到:simplecartjs.com
任何幫助將不勝感激,我也嘗試過其他形式的驗證技術,但由於simplecart存儲變量的方式,因此無法使用任何用於後期驗證的內容。請幫忙!!
謝謝你的時間和努力。
謝謝你指出這一點,增加額外的=已經停止彈出警報的功能,但現在添加到購物車功能根本沒有工作,我試圖把if語句放在我的不同地方。添加函數以及沒有運氣的me.checkout函數。任何關於這些代碼行應該去的地方的輸入將會很好。 – 2012-03-21 13:24:36
@ user1282169它看起來像你的代碼應該放在'checkQuantityAndPrice()'函數中的'CartItem'類中。 – DondeEstaMiCulo 2012-03-23 10:07:28