Javascript 如何将手动销售计算器脚本转换为自动销售计算器脚本?

Javascript 如何将手动销售计算器脚本转换为自动销售计算器脚本?,javascript,calculator,Javascript,Calculator,我有这个手册 <head> <script type="text/javascript"> function applyTax(){ var inputAmount = document.getElementById( 'dollars' ).value; var salesTaxGst = (inputAmount / 100) * 5; var salesTaxPst = (inputAmount / 100) *

我有这个手册

<head>
    <script type="text/javascript">
    function applyTax(){
      var inputAmount = document.getElementById( 'dollars' ).value;
      var salesTaxGst = (inputAmount / 100) * 5;
      var salesTaxPst =  (inputAmount / 100) * 9.975;
      var totalAmount = (inputAmount*1) + (salesTaxGst * 1) +(salesTaxPst * 1) ;

      document.getElementById( 'requestedAmount' ).innerHTML = inputAmount;
      document.getElementById( 'requestedTaxGst' ).innerHTML = salesTaxGst;
      document.getElementById( 'requestedTaxPst' ).innerHTML = salesTaxPst;
      document.getElementById( 'requestedTotal' ).innerHTML = totalAmount;
    }

    </script>
</head>

函数applyTax(){
var inputAmount=document.getElementById('$s').value;
var salesTaxGst=(输入量/100)*5;
var salesTaxPst=(输入量/100)*9.975;
var totalAmount=(inputAmount*1)+(salesTaxGst*1)+(salesTaxPst*1);
document.getElementById('requestedAmount')。innerHTML=inputAmount;
document.getElementById('requestedTaxGst')。innerHTML=salesTaxGst;
document.getElementById('requestedTaxPst')。innerHTML=salesTaxPst;
document.getElementById('requestedTotal')。innerHTML=totalAmount;
}
还有html

<body>

<input type="text" id="dollars" />
<input type="button" onclick="applyTax();" value="Calculate" />

<div id="requestedAmount"> </div>
<div id="requestedTaxGst"> </div>
<div id="requestedTaxPst"> </div>
<div id="requestedTotal"> </div>

</body>
</html> 

所以基本上这个脚本需要用户手动输入数据来计算税收。我正试图把它转换成一个自动销售税计算器,比如说一个电子商务网站

购物车页面:

    <div id="store_container">
            <div id="store">


                <%-- Left Navigation --%>
                <div style="text-align: center;" id="category">
                    <div id="items"><fmt:message key='summary'/>
                    </div>


                    <div class="cat_row">

                        <div style="text-align: left;"><span style="font-weight: bold;"><span
                                    style="background-color: rgb(239, 239, 239);"><fmt:message key='CartCategoryLabel1'/></span></span><br>


                            <%-- Start of Left Nav Widgets--%>
                            <ul style="list-style-type: none;">

                                <%-- clear cart widget --%>
                                <c:set var="value">
                                <c:choose>
                                <%-- if 'selectedCategory' session object exists, send user to previously viewed category --%>
                                        <c:when test="${!empty selectedCategory}">

                                        </c:when>

                                        <%-- otherwise send user to welcome page --%>
                                <c:otherwise>

                                    </c:otherwise>
                            </c:choose>
                         </c:set>                     
                <c:url var="url" value="${value}"/>
                <li><a href="viewCart?clear=true"><fmt:message key='ClearCart'/></a></li>

                    <%-- checkout widget --%>
                    <c:if test="${!empty cart && cart.numberOfItems != 0}">
                <li><a href="checkout"><fmt:message key='Checkout'/></a></li>
                </c:if>


                                <%-- continue shopping widget --%>
        <c:set var="value">
            <c:choose>
                <%-- if 'selectedCategory' session object exists, send user to previously viewed category --%>
                <c:when test="${!empty selectedCategory}">
                    category
                </c:when>
                <%-- otherwise send user to welcome page --%>
                <c:otherwise>
                    index.jsp
                </c:otherwise>
            </c:choose>
        </c:set>
                                <li style="background-color: rgb(198, 255, 201);"><a href="${value}"><fmt:message key='ContinueShopping'/></a></li>
                            </ul>

                            <br>
                            <span style="text-decoration: underline;"></span>
                        </div>
                    </div>
                    <%-- End of Left Navigation --%>


                <%-- Start of Cart Breadcrumbs --%>
                <div style="text-align: center;" id="thumbnails">
                    <div id="items">My shopping cart<br>
                    </div>
                    <br>

                    <div style="text-align: left; font-weight: bold;">
                        <div style="text-align: center;"><big></big></div>

                        <%-- Subtotal of cart--%>
                        <c:if test="${!empty cart && cart.numberOfItems != 0}">
                            <div style="text-align: center;" class="item_col"><big><fmt:message key='Subtotal'/> (${cart.subtotal})<br>
                                </big></div>
                            <big></big>
                        </c:if>
                        <%-- End Subtotal of cart--%>




                        <%-- Number of items in cart--%>
                        <div style="text-align: center;" class="item_col2">

                            <c:choose>
        <c:when test="${cart.numberOfItems > 1}">
            <big><fmt:message key='YouHave'/> (${cart.numberOfItems}) <fmt:message key='ItemsInYourCart'/><br></big>
        </c:when>
        <c:when test="${cart.numberOfItems == 1}">
            <big><fmt:message key='YouHave'/> (${cart.numberOfItems}) <fmt:message key='ItemsInYourCart'/><br></big>
        </c:when>
        <c:otherwise>
            <big><fmt:message key='EmptyShoppingCart'/><br></big>
        </c:otherwise>
    </c:choose>
                        </div>

                        <div style="text-align: center;"><big></big></div>
                    </div>
                    <big><br style="font-weight: bold;">
                    </big>
                    <%-- End of cart Breadcrumbs --%>       



                    <%-- Start of Products Table--%>        
                    <div style="font-weight: bold;" id="thumb_container"> 
                        <div style="text-align: left;">
                            <table style="text-align: left; width: 100%;" border="0" cellpadding="0"
                                   cellspacing="0">

                                <c:forEach var="cartItem" items="${cart.items}" varStatus="iter">
                                    <c:set var="product" value="${cartItem.product}"/>

                                    <tbody>
                                        <tr class="${((iter.index % 2) == 0) ? 'lightBlue' : 'white'}">
                                            <td
                                                style="vertical-align: middle; width: 196px; text-align: center; height: 140px;"><img class="img" src="${initParam.productImagePath}${product.name}.jpg"<br>
                                            </td>
                                            <td
                                                style="vertical-align: middle; width: 164px; text-align: center; height: 140px;">${product.name}<br>
                                            </td>
                                            <td

                                                style="vertical-align: middle; width: 100px; text-align: center; height: 140px;">${product.price}<br>
                                            </td>
                                            <td
                                                style="vertical-align: middle; width: 180px; text-align: center; height: 140px;"><form action="addToWishlist" method="post"><br><br> <input
name="productId" value="${product.id}" type="hidden"> <input class="submit"
value="Add To Wishlist" type="submit"></form><br>
                                            </td>
                                            <td
                                                style="vertical-align: middle; text-align: center; height: 140px; width: 180px; padding-top:35px;"><form action="updateCart" method="post">
                                                    <input type="hidden"
                                                           name="productId"
                                                           value="${product.id}">

                                                    <input type="text"
                                                           maxlength="2"
                                                           size="2"
                                                           value="${cartItem.quantity}"
                                                           name="quantity"
                                                           style="margin:0px">

                                                    <input class="updateButton"  type="submit"
                                                           name="submit"
                                                           value="update">

                                                </form><br>
                                            </td>
                                        </tr>
                                    </tbody>

                                </c:forEach>
                            </table>


                            <%-- End of Products Table--%>
function applyTax(){
  var cartSubTotal = parseFloat(document.getElementById( 'cartSubtotal' ).innerHTML);
  console.log(cartSubTotal);
  var salesTaxGst = (cartSubTotal / 100) * 5;
  var salesTaxPst =  (cartSubTotal / 100) * 9.975;
  var totalAmount = (cartSubTotal*1) + (salesTaxGst * 1) +(salesTaxPst * 1) ;

  document.getElementById( 'cartSubtotal' ).innerHTML = cartSubTotal.toFixed(2);
  document.getElementById( 'taxGst' ).innerHTML = salesTaxGst.toFixed(2);
  document.getElementById( 'taxPst' ).innerHTML = salesTaxPst.toFixed(2);
  document.getElementById( 'cartTotal' ).innerHTML = totalAmount.toFixed(2);
}

// Call the applyTax() when the window finishes loading...
window.onload = function() {
  applyTax();
}


  • 类别 index.jsp 背景色:rgb(198255201);“>

我的购物车

(${cart.subtotal})
(${cart.numberOfItems})
(${cart.numberOfItems})


${product.name}
${product.price}




所以我在想,为了将脚本转换为自动计算购物车小计,我只需要声明不同的变量来改变一些事情

例如:

<head>
<script type="text/javascript">
function applyTax(){
  var ${cart.subtotal} = document.getElementById( 'cartSubtotal' ).value;
  var salesTaxGst = (${cart.subtotal} / 100) * 5;
  var salesTaxPst =  (${cart.subtotal} / 100) * 9.975;
  var totalAmount = (${cart.subtotal}*1) + (salesTaxGst * 1) +(salesTaxPst * 1) ;

  document.getElementById( 'cartSubtotal' ).innerHTML = ${cart.subtotal} ;
  document.getElementById( 'taxGst' ).innerHTML = salesTaxGst;
  document.getElementById( 'taxPst' ).innerHTML = salesTaxPst;
  document.getElementById( 'cartTotal' ).innerHTML = totalAmount;
}
</script>
</head>


<html>
<body>

<input type="text" id="cartSubtotal" />
<input type="button" onload="applyTax();" value="Calculate" />

<div id="cartSubtotal"> </div>
<div id="taxGst"> </div>
<div id="taxPst"> </div>
<div id="cartTotal"> </div>

</body>
</html>

函数applyTax(){
var${cart.subtotal}=document.getElementById('cartSubtotal').value;
var salesTaxGst=(${cart.subtotal}/100)*5;
var salesTaxPst=(${cart.subtotal}/100)*9.975;
var totalAmount=(${cart.subtotal}*1)+(salesTaxGst*1)+(salesTaxPst*1);
document.getElementById('cartSubtotal')。innerHTML=${cart.subtotal};
document.getElementById('taxGst')。innerHTML=salesTaxGst;
document.getElementById('taxPst')。innerHTML=salesTaxPst;
document.getElementById('cartTotal')。innerHTML=totalAmount;
}
------编辑----

为了获得更好的视觉效果,这是Forever 21结账过程的屏幕截图。您可以看到,在页面的末尾,它们显示了
总金额=购物车小计+税+运费
。这正是我试图用脚本做的

快照:

我想这就是你想要做的:


函数applyTax(){
var cartSubtotal=parseFloat(document.getElementById('cartSubtotal').value);
如果(!isNaN(小计)){
var salesTaxGst=(小计/100)*5;
var salesTaxPst=(cartSubtotal/100)*9.975;
var totalAmount=cartSubtotal+salesTaxGst+salesTaxPst;
document.getElementById('cartSubtotal')。innerHTML=“Subtotal:”+cartSubtotal;
document.getElementById('taxGst').innerHTML=“销售税GST:”+salesTaxGst;
document.getElementById('taxPst').innerHTML=“销售税PST:”+salesTaxPst;
document.getElementById('cartTotal').innerHTML=“总amo
<div class="main">
  <h1>Cart</h1>
  <p>Cart Subtotal: $<span id="cartSubtotal">100.00</span></p>
  <p>Sales Tax GST: $<span id="taxGst"></span></p>
  <p>Sales Tax PST: $<span id="taxPst"></span></p>
  <p>Final Total: $<span id="cartTotal"></span></p>
</div>
Cart

Cart Subtotal: $100.00
Sales Tax GST: $5.00
Sales Tax PST: $9.97
Final Total: $114.97