2011-04-18 105 views
0

好吧,基本上我已經被賦予了爲我們生成付款頁面的任務。 我有一個小問題。我的老闆希望我整合谷歌結帳,並且我工作得很好。 他還希望我定義一個最小訂單。如何比較JavaScript中的最小訂單數量值

例如:我們在「11.00£」賣每標籤「1000」,但最低發票「£33.00」

我有我的設置頁面4盒。一個是數量(Quantity),另一個是成本(Price)和兩個隱藏字段。 其中一個領域是谷歌結帳(產品價格) 最後一場是我放在那裏的隱藏領域,試圖比較價格和更新成本。

目前,我有我的量的變化文本框onChange事件,這樣的:

function labels_1() { 
    var cost; 
    var costper1k = '11.00'; 
    var qty = orderform.labels_1_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_1_cost.value = "Not a Valid Quantity"; 
    } 
    else if (qty < 0) { 
     orderform.labels_1_cost.value = "Negative Quantity"; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
    } 

    orderform.labels_1_cost.value = formatCurrency(cost); 
    orderform.hiddenprice_1.value = cost; 
    orderform.prodprice_1.value = orderform.labels_1_cost.value; 
    orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value; 
} 

這是運作良好,也無最低發票金額的驗證。

我有這6個功能,都是這樣,因爲我們賣6種標籤類型。正如我所說的,谷歌結帳工作正常與所有這些。

我試圖創建此:

function hidden_1() { 
    var cost = '33.00'; 
    var originalcost = orderform.labels_1_cost.value; 
    var a = Number(orderform.hidden_1.value); 
    var b = Number(orderform.hidden_2.value); 
    var c = Number(orderform.hidden_3.value); 
    var d = Number(orderform.hidden_4.value); 
    var e = Number(orderform.hidden_5.value); 
    var f = Number(orderform.hidden_6.value); 
    var total = a+b+c+d+e+f; 
    if (total.value < '33'){ 
    orderform.labels_1_cost.value = formatCurrency(cost);} 
    if (total.value > '33') { 
    orderform.labels_1_cost.value = originalcost;} 

} 

和追加hidden_​​1();到label_1()函數的末尾。

它不起作用,我正在試圖修理它。我甚至不確定我是這麼做的。

任何人都可以提供任何幫助嗎? 非常感謝

編輯

對不起,這裏是我的formatCurrency功能:

function formatCurrency(num) { 
num = num.toString().replace(/\$|\,/g,''); 
if(isNaN(num)) 
num = "0"; 
sign = (num == (num = Math.abs(num))); 
num = Math.floor(num*100+0.50000000001); 
cents = num%100; 
num = Math.floor(num/100).toString(); 
if(cents<10) 
cents = "0" + cents; 
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
num = num.substring(0,num.length-(4*i+3))+','+ 
num.substring(num.length-(4*i+3)); 
return (((sign)?'':'-') + '£' + num + '.' + cents); 

}

全部站點代碼: Order.php:

<html> 
<head> 
<style type="text/css" media="screen">@import "css/forms-style.css";</style> 
<script type="text/javascript" src="js/labels.js"></script> 
</head> 

<script id="googlecart-script" type="text/javascript" 
    src="http://checkout.google.com/seller/gsc/v2/cart.js?mid=REMOVED!!!" 
    currency="GBP" 
    highlight-time="5000" 
    highlight-color="#FF7878" 
    cart-opening-time="500" 
    hide-cart-when-empty="true" 
    close-cart-when-click-away="false" 
    post-cart-to-sandbox="true"> 
</script> 

<body> 
<div id="wrapper"> 

<form name="orderform"> 
<!-- ----------------------------------------------------------------------- --> 
<div id="content"> 
<div class="heading-section"> 
<h2>Printed Labels</h2> 
<p>All Printed Labels come outside wound, with a standard <strong>10 item setups per roll.</strong></p> 
<p>The minimum Invoice amount is <strong>&pound;30.00</strong></p> 
</div> 
<div align="center" id="statement">Hover Over Images To Zoom</div> 
<br/> 
<p> 
    <?php tableformat('1', '26x44mm (4a) 76mm Cores','26x44mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in <font color=red><strong>RED</strong></font> ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: <select class="product-attr-color"> 
      <option>Normal</option> 
      <option>Special Buy</option> 
     <option>Special Purchase</option> 
     <option>Opaque Backing Material</option> 
     </select>','img/img_1.png','img/img_7.png','img/img_8.png','img/img_1_thumb.png','img/img_7_thumb.png','img/img_8_thumb.png'); ?> 
    <?php tableformat('2', '44x26mm (2a) 76mm Cores','44x26mm Labels. Custom options are available for these labels. They can include the text SPECIAL BUY or SPECIAL PURCHASE overprinted in <font color=red><strong>RED</strong></font> ink. These labels can also include a Country of Origin and the John lewis Address. The labels are also available in blockout (opaque) backing, to be used for repricing, or generally covering other labels.','Style: <select class="product-attr-color"> 
      <option>Normal</option> 
      <option>Special Buy</option> 
     <option>Special Purchase</option> 
     <option>Opaque Backing Material</option> 
     </select>','img/img_2.png','','','img/img_2_thumb.png','',''); ?> 
    <?php tableformat('3', '35x13mm (2a) 76mm Cores','35x13mm Labels. These Labels can include a Country of Origin. Please Note, the size of the label is fairly limited','','img/img_3.png','','','img/img_3_thumb.png','',''); ?> 
    <?php tableformat('4', '80x10mm [dumbells] (1a) 76mm Cores','80x10mm Dumbell Labels. These Labels can be produced with either an EAN8 or an EAN13 consumer code','','img/img_4.png','','','img/img_4_thumb.png','',''); ?> 
    <?php tableformat('5', '25x10mm [price only] (4a) 76mm Cores','25x10mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_5.png','','','img/img_5_thumb.png','',''); ?> 
    <?php tableformat('6', '15x8mm [price only] (4a) 76mm Cores','15x8mm Labels. These Small labels may include a Price Only. No text will be printed on these labels. If you require Text, please look at some of the other sizes above','','img/img_6.png','','','img/img_6_thumb.png','',''); ?> 
</p> 
<div class="heading-section" align="right"> 
<input type="reset"> 

<!-- ----------------------------------------------------------------------- --> 
</hr> 
</h2> 
</div> 
</div> 
</form> 

<br/><br/> 
</div> 
</div> 
</body> 
</html> 

<?php 

function tableformat($num, $label, $labeldesc, $options, $pic1, $pic2, $pic3, $thumb1, $thumb2, $thumb3) { 

echo 
'<div id="'.$num.'" class="section"> 
<div class="product"> 
<table border=0 width=100% align=center class="product-table"> 
<tr valign=top bgcolor=3366cc> 
     <td colspan=4><span class="product-name">'.$label.'</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class="p1" href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb1.'" alt=""><b><img class="large" onerror="this.style.display=\'none\'" src="'.$pic1.'" alt=""></b></a>&nbsp;&nbsp;<a class="p1" onerror="this.style.display=\'none\'" ;href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb2.'" alt=""><b><img onerror="this.style.display=\'none\'" class="large" src="'.$pic2.'" alt=""></b></a>&nbsp;&nbsp;<a class="p1" href="#v"><img onerror="this.style.display=\'none\'" src="'.$thumb3.'" alt=""><b><img class="large" onerror="this.style.display=\'none\'" src="'.$pic3.'" alt=""></b></a></td> 
     <input type="hidden" name="productname_'.$num.'" class="product-title" value="0"/> 
    </tr> 
    <tr><td>&nbsp;</td> 
    </tr> 
    <tr> 
    <td align="left" colspan="2"> 
     <span id="labels_'.$num.'_exp">'.$labeldesc.'</span> 
    </td> 
    </tr> 
    <tr><td>&nbsp;</td> 
    </tr> 
    <tr valign=center align=right> 
    <td width=30%> 
    &nbsp; 
    </td> 
    <td width=auto> 
    '.$options.' 
    <strong>Quantity:</strong> <input type="text" name="labels_'.$num.'_qty" size="10" value=0 onChange="OnChan();"> 
    <strong>Price:</strong> <input type="text" name="labels_'.$num.'_cost" class="form-cost" size="10" value="0.00" onChange="OnChan();"> 
    <input type="hidden" name="prodprice_'.$num.'" class="product-price" value="£0.00"/> 
    <input type="hidden" name="hiddenprice_'.$num.'" value="0.00"/> 
    </td> 
    </tr> 
    <tr> 
    <td>&nbsp;</td> 
    </tr> 
    <td width=30%> 
    &nbsp; 
    </td> 
    <td width=auto align=right> 
    <div role="button" alt="Add to cart" tabindex="0" class="googlecart-add-button" onClick="hidden_'.$num.';"> 
    </div> 
    </td> 
    </tr> 
    </table> 
<br/> 
<br/> 

</div></div>'; 


} // tableformat 

?> 

個labels.js是在這裏:

function formatCurrency(num) { 
    num = num.toString().replace(/\$|\,/g,''); 
    if(isNaN(num)) 
    num = "0"; 
    sign = (num == (num = Math.abs(num))); 
    num = Math.floor(num*100+0.50000000001); 
    cents = num%100; 
    num = Math.floor(num/100).toString(); 
    if(cents<10) 
    cents = "0" + cents; 
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) 
    num = num.substring(0,num.length-(4*i+3))+','+ 
    num.substring(num.length-(4*i+3)); 
    return (((sign)?'':'-') + '£' + num + '.' + cents); 
} 

function isPositiveInteger(val){ 
    if(val==null){return false;} 
    if (val.length==0){return false;} 
    for (var i = 0; i < val.length; i++) { 
     var ch = val.charAt(i) 
     if (ch < "0" || ch > "9") { 
     return false 
     } 
    } 
    return true; 
} 

/* ----------------------------------------------------------------------------- 
    LABELS - 1 
----------------------------------------------------------------------------- */ 

function labels_1() { 
    var cost, desc; 
    var costper1k = '11.00'; 
    var qty = orderform.labels_1_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_1_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_1_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 

    orderform.labels_1_cost.value = formatCurrency(cost); 
    alert("test - end of normal"); 
    hidden_1(); 
} 


/* ----------------------------------------------------------------------------- 
    Hidden - 1 
----------------------------------------------------------------------------- */ 
function hidden_1() { 
    alert("starting"); 
    var costed = '33.00'; 
    var originalcost = orderform.labels_1_cost.value; 
    var a = Number(orderform.hidden_1.value); 
    var b = Number(orderform.hidden_2.value); 
    var c = Number(orderform.hidden_3.value); 
    var d = Number(orderform.hidden_4.value); 
    var e = Number(orderform.hidden_5.value); 
    var f = Number(orderform.hidden_6.value); 
    var total = a+b+c+d+e+f; 
    if (total < 33){ 
    orderform.labels_1_cost.value = costed;} 
    if (total => 33) { 
    orderform.labels_1_cost.value = originalcost;} 
    alert("ending"); 
} 


/* ----------------------------------------------------------------------------- 
    LABELS - 2 
----------------------------------------------------------------------------- */ 




function labels_2() { 
    var cost, desc; 
    var costper1k = '11.00'; 
    var qty = orderform.labels_2_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_2_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_2_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 
    orderform.labels_2_cost.value = formatCurrency(cost); 
} 

/* ----------------------------------------------------------------------------- 
    LABELS - 3 
----------------------------------------------------------------------------- */ 

function labels_3() { 
    var cost, desc; 
    var costper1k = '11.00'; 
    var qty = orderform.labels_3_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_3_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_3_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else{ 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 
    orderform.labels_3_cost.value = formatCurrency(cost); 
} 

/* ----------------------------------------------------------------------------- 
    LABELS - 4 
----------------------------------------------------------------------------- */ 

function labels_4() { 
    var cost, desc; 
    var costper1k = '13.75'; 
    var qty = orderform.labels_4_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_4_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_4_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 
    orderform.labels_4_cost.value = formatCurrency(cost); 
} 

/* ----------------------------------------------------------------------------- 
    LABELS - 5 
----------------------------------------------------------------------------- */ 

function labels_5() { 
    var cost, desc; 
    var costper1k = '7.89'; 
    var qty = orderform.labels_5_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_5_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_5_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 
    orderform.labels_5_cost.value = formatCurrency(cost); 
} 


/* ----------------------------------------------------------------------------- 
    LABELS - 6 
----------------------------------------------------------------------------- */ 
function labels_6() { 
    var cost, desc; 
    var costper1k = '10.14'; 
    var qty = orderform.labels_6_qty.value; 
    if (isNaN(qty)) { 
     orderform.labels_6_cost.value = "Not a Valid Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else if (qty < 0) { 
     orderform.labels_6_cost.value = "Negative Quantity"; 
     desc = 'Not a Valid Quantity'; 
    } 
    else { 
     cost = ((qty/1000)*costper1k); 
     desc = qty+' ordered at a cost of '+formatCurrency(cost); 
     } 
    orderform.labels_6_cost.value = formatCurrency(cost); 
} 

/* ----------------------------------------------------------------------------- 
    OnChan (Changes) 
----------------------------------------------------------------------------- */ 

function OnChan() { 
labels_1(); 
labels_2(); 
labels_3(); 
labels_4(); 
labels_5(); 
labels_6(); 
orderform.prodprice_1.value=orderform.labels_1_cost.value; 
orderform.prodprice_2.value=orderform.labels_2_cost.value; 
orderform.prodprice_3.value=orderform.labels_3_cost.value; 
orderform.prodprice_4.value=orderform.labels_4_cost.value; 
orderform.prodprice_5.value=orderform.labels_5_cost.value; 
orderform.prodprice_6.value=orderform.labels_6_cost.value; 
orderform.productname_1.value='26x44mm (4a) 76mm Cores'+' x '+orderform.labels_1_qty.value; 
orderform.productname_2.value='44x26mm (2a) 76mm Cores'+' x '+orderform.labels_2_qty.value; 
orderform.productname_3.value='35x13mm (2a) 76mm Cores'+' x '+orderform.labels_3_qty.value; 
orderform.productname_4.value='80x10mm [dumbells] (1a) 76mm Cores'+' x '+orderform.labels_4_qty.value; 
orderform.productname_5.value='25x10mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_5_qty.value; 
orderform.productname_6.value='15x8mm [price only] (4a) 76mm Cores'+' x '+orderform.labels_6_qty.value; 

} 

和測試網站是安裝在:

http://mrotacon.heliohost.org/testing/order.php

也 - 在這裏是沒有比較,工作正常的測試版...但我需要最小發票金額

http://mrotacon.heliohost.org/testing/orderworking.php

+0

你的代碼太溼了。你應該嘗試尋找一種方法來製作乾燥(不重複自己)。 – danniel 2011-04-18 19:02:53

+0

我會的,別擔心 - 現在只需要一個快速頁面,當我有一些實際的空閒時間時,我會改變它 – Dave 2011-04-18 19:11:09

回答

0

你不說怎麼不工作和你有什麼診斷BU T ...可能是因爲你比較字符串而不是數字值?例如,'6'>'33'。這個「total.value」對我來說也看起來很狡猾。

+0

基本上它不會更新orderform.labels_1_cost.value,因爲我打算這樣做。它保持orderform.labels_1_cost.value的原始值。 例如: 如果我在數量中輸入500 - 價格是£5.50 我希望它看到它低於最低發票順序£33.00,並將值更改爲此值。 它在orderform.labels_1_cost.value,orderform.labels_2_cost.value,orderform.labels_3_cost.value,orderform.labels_4_cost.value,orderform.labels_5_cost.value和orderform.labels_6_cost.value 這些所有默認值在比較值=「0.00」,並隨數量變化 – Dave 2011-04-18 17:56:11

+0

嘗試擺脫使用總數的.value,並在條件標準中設定'33'33,然後放棄它。 – 2011-04-18 17:58:36

+0

你應該使它''=''而不是'>'(否則當它完全是33時會發生什麼?)。 – 2011-04-18 18:05:40