如何比较JavaScript中的最小订单数量的值

时间:2011-04-18 17:37:29

标签: javascript

好的,基本上我已经完成了为我们制作付款页面的任务。 我有一点问题。我的老板要我整合google checkout,我的工作正常。 他还希望我定义最低订单。

例如:我们以“每张1000英镑”的价格出售标签,但最低发票为“£33.00”

我已经设置了4个方框的页面。一个是数量(数量),另一个是成本(价格)和两个隐藏的字段。 其中一个领域是谷歌结账(产品价格) 最后一个字段是我放在那里的隐藏字段,试图比较价格并更新成本。

目前,我有关于数量更改文本框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

1 个答案:

答案 0 :(得分:0)

你没有说它是如何工作的以及你得到的诊断但是......可能是因为你在比较字符串而不是数值?因此,例如,'6'&gt; '33'。那个“total.value”对我来说也很狡猾。