使用Javascript中的百分比更正总金额

时间:2016-02-20 03:08:20

标签: javascript

我有一个Javascript文件会自动计算总数加百分比。首先我认为我的代码是正确的。

javascript.js

  function getPayPalProcessingFee() {
     var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
     qty = parseInt(qty);

     //var cat_buttons = jQuery("input[name=cat]")
     //var current_index = cat_buttons.index(cat_buttons.find(':checked'));
     var current_index = $("input[name=cat]:checked").attr('id');

     switch (current_index){
         case 'cat-both':
            return 3*qty;
            break;
         case 'cat-aerial':
            return 1.8*qty;
            break;
         case 'cat-loto':
            return 1.8*qty;
            break;
         default:
            return 0;
     }
 }

function trainthetrainerForm_calculateItemAmount() {
    var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
    var current_val = jQuery("input[name=cat]:checked").val();

    var amount = parseInt(qty) * current_val;
    amount = parseFloat(amount).toFixed(2);
    return amount;
}

function trainthetrainerForm_calculateFee() {
    var fee = getPayPalProcessingFee();
    return fee;
}

function trainthetrainerForm_displayTotalAmount() {
    var amount = trainthetrainerForm_calculateItemAmount();
    console.log(amount);        
            var fee = getPayPalProcessingFee();
    console.log(fee);        
            var totalamount = parseFloat(amount) + parseFloat(fee);
    console.log(totalamount);        
            totalamount = parseFloat(totalamount).toFixed(2);
    console.log(totalamount);

    jQuery('#paypal_submit_form input[name=tmp_total_amount]').val(totalamount);
}

function submitTrainthetrainerForm() {
    var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
    jQuery('#paypal_submit_form input[name=quantity_1]').val(qty);

    var totalAmount = 0;
    var amount = trainthetrainerForm_calculateItemAmount();
    var processingFee = trainthetrainerForm_calculateFee();

    totalAmount = amount + processingFee;

    jQuery('#paypal_submit_form input[name=business]').val('shana@same.org');
    jQuery('#paypal_submit_form input[name=amount]').val(totalAmount);
    jQuery('#paypal_submit_form input[name=amount_2]').val(processingFee);
    jQuery('#paypal_submit_form').submit();
    return true;
}

jQuery(document).ready(function (){
    $(document).on('change',$("#bal_number_of_members"),function(){
        trainthetrainerForm_displayTotalAmount();
    });
    jQuery("#bal_submit_btn").click(function() {
        submitTrainthetrainerForm();
    });

    trainthetrainerForm_displayTotalAmount();
});

form.html

   <h2 class="contentheading" style="margin-top: 0px;">2016 “Train-The-Trainer” Workshops<br /><br /> AERIAL</h2>
 <div>
   <p>$50.00 per class/person<br />Sign up for both for $90.00</p>
 </div>
 <div>
  <form id="paypal_submit_form" https://www.paypal.com/cgi-bin/webscr" method="post">
    <input name="cmd" type="hidden" value="_cart" /> 
    <input name="upload" type="hidden" value="1" /> 
    <input name="charset" type="hidden" value="utf8" /> 
    <input name="business" type="hidden" /> 
    <input name="currency_code" type="hidden" value="USD" /> 
    <input name="custom" type="hidden" /> 
    <input name="amount" type="hidden" /> 
    <input name="first_name" type="hidden" /> 
    <input name="last_name" type="hidden" /> 
    <input name="address1" type="hidden" /> 
    <input name="city" type="hidden" /> 
    <input name="state" type="hidden" /> 
    <input name="zip" type="hidden" /> 
    <input name="email" type="hidden" /> 
    <input name="night_phone_b" type="hidden" /> 
    <input name="address_override" type="hidden" value="1" />
    <div id="paypal_prs" style="font-size: 12px;">
        <p>
            <input id="cat-both" checked="checked" name="cat" type="radio" value="90" />
                <label for="cat-both">Both</label> 
            <input id="cat-aerial" name="cat" type="radio" value="50" />
                <label for="cat-aerial">Aerial</label> 
            <input id="cat-loto" name="cat" type="radio" value="50" />
                <label for="cat-loto">Lockout/Tagout</label>
        </p>
        <br /> Members:
        <select id="bal_number_of_members" style="font-size: 12px; padding: 3px;" name="number_of_members">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
            <option value="11">11</option>
        </select>
        <br /> Total Amount (Cost + Processing Fee): 
            <input style="width: 50px; margin-right: 10px; padding: 2px; padding-bottom: 1px; font-size: 12px;" name="tmp_total_amount" readonly="readonly" type="text" value="93" /> 
            <input id="bal_submit_btn" style="padding: 3px 5px; font-size: 12px; cursor: pointer;" type="button" value="Pay Here" /> 
            <input name="item_name_1" type="hidden" value="2016 Train-The-Trainer Workshops(AERIAL LIFTS &amp; LOCKOUT/TAGOUT)" /> 
            <input name="amount_1" type="hidden" value="90" /> 
            <input name="quantity_1" type="hidden" value="1" /> 
            <input name="item_name_2" type="hidden" value="Processing fee" /> 
            <input name="amount_2" type="hidden" value="0" /> 
            <input name="quantity_2" type="hidden" value="1" />
    </div>
    <input name="notify_url" type="hidden" value="http://some.org/tmp_ipn.php" /> 
    <input name="return" type="hidden" value="http://some.org/" /> 
    <input name="cancel_return" type="hidden" value="http://some.org/index.php?view=article&amp;id=278" /> 
    <input name="no_shipping" type="hidden" value="1" />
   </form>
   </div>
  <div style="font-size: 11px; margin-top: 10px; color: red;">
    Additional 2.9% + $0.30 processing fee will be charged with all orders paid by credit card.
   </div>
   <div style="margin-top: 50px;">
    <a style="font-size: 18px;" href="images/Flyer_2016.pdf" target="_blank">Download Order Form</a>
  </div>
 <p style="font-size: 14px;">
    <strong>Please fax or email the order form to the office.</strong>
 </p>

如果用户检查输入id = both,则该值将乘以select id = bal_number_of_members的选项值。

否则,如果用户单击输入id = cat-aeral并输入id = cat-loto,则值wold将乘以select id = bal_number_of_members的选项值。

通过此设置,如果我检查id = both,则选择id = bal_number_of_members

中的选项值
 1 = 90 //value of 90 + 3
 2 = 186.00 //93 * 2
 3 = 279.00
 4 = 372.00
 5 = 465.00
 6 = 558.00
 7 = 651.00
 8 = 744.00
 9 = 873.00

我们的会计主管通知此计算不正确 它应该是

 1 = 93.00 //3.00 
 2 = 185.68  // 5.68
......

如何正确获得百分比总数?

这就是她想要的

   Additional 2.9% + $0.30 processing fee will be charged with all orders paid by credit card.

1 个答案:

答案 0 :(得分:2)

以下是解决方案JS Fiddle

的工作示例

&#13;
&#13;
  function trainthetrainerForm_calculateItemAmount() {
    var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
    var current_val = jQuery("input[name=cat]:checked").val();
		//console.log(current_val);
    var amount = parseInt(qty) * current_val;
    amount = parseFloat(amount).toFixed(2);
    return amount;
  }

  
  function trainthetrainerForm_displayTotalAmount() {
    var amount = trainthetrainerForm_calculateItemAmount();
    console.log(parseFloat(amount));
    var totalamount = parseFloat(amount)+((parseFloat(amount)*0.029)+0.30);
    console.log(totalamount);
    totalamount = parseFloat(totalamount).toFixed(2);
    console.log(totalamount);

    jQuery('#paypal_submit_form input[name=tmp_total_amount]').val(totalamount);
  }

  function submitTrainthetrainerForm() {
    var qty = jQuery('#paypal_submit_form select[name=number_of_members]').val();
    jQuery('#paypal_submit_form input[name=quantity_1]').val(qty);

    var totalAmount = 0;
    var amount = trainthetrainerForm_calculateItemAmount();
    var processingFee = trainthetrainerForm_calculateFee();

    totalAmount = amount + processingFee;

    jQuery('#paypal_submit_form input[name=business]').val('shana@same.org');
    jQuery('#paypal_submit_form input[name=amount]').val(totalAmount);
    jQuery('#paypal_submit_form input[name=amount_2]').val(processingFee);
    jQuery('#paypal_submit_form').submit();
    return true;
  }

  jQuery(document).ready(function() {
    $(document).on('change', $("#bal_number_of_members"), function() {
      trainthetrainerForm_displayTotalAmount();
    });
    jQuery("#bal_submit_btn").click(function() {
      submitTrainthetrainerForm();
    });

    trainthetrainerForm_displayTotalAmount();
  });
&#13;
<script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
<h2 class="contentheading" style="margin-top: 0px;">2016 “Train-The-Trainer” Workshops<br /><br /> AERIAL</h2>
<div>
  <p>$50.00 per class/person
    <br />Sign up for both for $90.00</p>
</div>
<div>
  <form id="paypal_submit_form" action="https://www.paypal.com/***" method="post">
    <input name="cmd" type="hidden" value="_cart" />
    <input name="upload" type="hidden" value="1" />
    <input name="charset" type="hidden" value="utf8" />
    <input name="business" type="hidden" />
    <input name="currency_code" type="hidden" value="USD" />
    <input name="custom" type="hidden" />
    <input name="amount" type="hidden" />
    <input name="first_name" type="hidden" />
    <input name="last_name" type="hidden" />
    <input name="address1" type="hidden" />
    <input name="city" type="hidden" />
    <input name="state" type="hidden" />
    <input name="zip" type="hidden" />
    <input name="email" type="hidden" />
    <input name="night_phone_b" type="hidden" />
    <input name="address_override" type="hidden" value="1" />
    <div id="paypal_prs" style="font-size: 12px;">
      <p>
        <input id="cat-both" checked="checked" name="cat" type="radio" value="90" />
        <label for="cat-both">Both</label>
        <input id="cat-aerial" name="cat" type="radio" value="50" />
        <label for="cat-aerial">Aerial</label>
        <input id="cat-loto" name="cat" type="radio" value="50" />
        <label for="cat-loto">Lockout/Tagout</label>
      </p>
      <br /> Members:
      <select id="bal_number_of_members" style="font-size: 12px; padding: 3px;" name="number_of_members">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8">8</option>
        <option value="9">9</option>
        <option value="10">10</option>
        <option value="11">11</option>
      </select>
      <br /> Total Amount (Cost + Processing Fee):
      <input style="width: 50px; margin-right: 10px; padding: 2px; padding-bottom: 1px; font-size: 12px;" name="tmp_total_amount" readonly="readonly" type="text" value="93" />
      <input id="bal_submit_btn" style="padding: 3px 5px; font-size: 12px; cursor: pointer;" type="button" value="Pay Here" />
      <input name="item_name_1" type="hidden" value="2016 Train-The-Trainer Workshops(AERIAL LIFTS &amp; LOCKOUT/TAGOUT)" />
      <input name="amount_1" type="hidden" value="90" />
      <input name="quantity_1" type="hidden" value="1" />
      <input name="item_name_2" type="hidden" value="Processing fee" />
      <input name="amount_2" type="hidden" value="0" />
      <input name="quantity_2" type="hidden" value="1" />
    </div>
    <input name="notify_url" type="hidden" value="http://some.org/tmp_ipn.php" />
    <input name="return" type="hidden" value="http://some.org/" />
    <input name="cancel_return" type="hidden" value="http://some.org/index.php?view=article&amp;id=278" />
    <input name="no_shipping" type="hidden" value="1" />
  </form>
</div>
<div style="font-size: 11px; margin-top: 10px; color: red;">
  Additional 2.9% + $0.30 processing fee will be charged with all orders paid by credit card.
</div>
<div style="margin-top: 50px;">
  <a style="font-size: 18px;" href="images/Flyer_2016.pdf" target="_blank">Download Order Form</a>
</div>
<p style="font-size: 14px;">
  <strong>Please fax or email the order form to the office.</strong>
</p>
&#13;
&#13;
&#13;

您需要先为所选数量计算基本金额,然后对该金额应用以上费用。例如,

如果用户选择了“&#39;和不。如果成员为2,那么基本金额将为180.现在将费用应用于此基数金额 (180 * 0.029)+0.30 = 5.52。所以你的总金额将是185.52

你在current_index中所做的事情似乎不正确。额外的2.9%+ 0.30应适用于总基数,即90(对于两者)*数量