2012-02-27 3 views
0

먼저 한 후 카트 총을 추가하는 것은, 여기 : 누군가가 오른쪽 하단에있는 "소계"를 업데이트 할 수 있습니다 항목의 수량을 업데이트 할 때 http://webapps.bcit.ca/A00839579/MDIA3207/Assign4/cart.html자바 스크립트 : 수량 변화

기본적으로, 내가 좋아하는 것입니다. 너무 열심히해서는 안되지만, 웬일인지 그것을 이해할 수는 없습니다. 그리고 부수적으로, 수량이 업데이트 될 때 어떻게 소수를 사용하여 가격을 유지할 수 있는지 파악할 수 있다면 좋을 것입니다. 정말 고마워!

var artprice = 28.99; 
var artquantity = document.getElementById("artquantity").value; 
var arttotal = (artprice * artquantity).toFixed(2) - 0; 

var loverprice = 19.95; 
var loverquantity = document.getElementById("loverquantity").value; 
var lovertotal = (loverprice * loverquantity).toFixed(2) - 0; 

var nightprice = 32.00; 
var nightquantity = document.getElementById("nightquantity").value; 
var nighttotal = (nightprice * nightquantity).toFixed(2) - 0; 


function artupdate() { 
var subtotal = arttotal + lovertotal + nighttotal; 

var artprice = 28.99; 
var artquantity = document.getElementById("artquantity").value; 
var arttotal = (artprice * artquantity).toFixed(2) - 0; 

document.getElementById("artprice").innerHTML = "$" + arttotal; 
document.getElementById("subtotal").innerHTML = "$" + subtotal; 
} 

function loverupdate() { 
var subtotal = arttotal + lovertotal + nighttotal; 

var loverprice = 19.95; 
var loverquantity = document.getElementById("loverquantity").value; 
var lovertotal = (loverprice * loverquantity).toFixed(2) - 0; 

document.getElementById("loverprice").innerHTML = "$" + lovertotal; 
document.getElementById("subtotal").innerHTML = "$" + subtotal; 
} 

function nightupdate() { 
var subtotal = arttotal + lovertotal + nighttotal; 

var nightprice = 32.00; 
var nightquantity = document.getElementById("nightquantity").value; 
var nighttotal = (nightprice * nightquantity).toFixed(2) - 0; 

document.getElementById("nightprice").innerHTML = "$" + nighttotal; 
document.getElementById("subtotal").innerHTML = subtotal; 
} 

var subtotal = arttotal + lovertotal + nighttotal; 
document.getElementById("subtotal").innerHTML = "$" + subtotal; 

function isNumberKey(evt) 
{ 
var charCode = (evt.which) ? evt.which : event.keyCode 
if (charCode > 31 && (charCode < 48 || charCode > 57)) 
return false; 

return true; 
} 
+0

제거 -0이 0 내가 종류의 내 주요 문제를 파악 – mplungjan

+0

후행 한 경우에서는 toFixed을 취소하지만이 변경되면되는 첫 번째 숫자의 수량은 $ 28.99이고 1 대신 3 개의 수량이됩니다.이 값은 86.97 + 19.95 + 32.00이고 138.92와 같습니다. 그러나 첫 번째 숫자의 수량을 3으로 변경하고 첫 번째 숫자를 변경 한 후에 두 번째 숫자를 3의 수량으로 변경하면 3 개의 수량과 다른 두 개의 중간을 1 개의 수량으로 추가합니다 . 0을 제거하면이 "28.9919.9532.00"과 같은 3 개의 숫자가 소계로 추가됩니다. 그러나 나는 그 모든 숫자가 합쳐지기를 원하고 두 개의 십진법을 병행하지 않고 그와 나란히 놓기를 원합니다. –

답변

1

에 부하를 문서화하는 다른 기능을 추가 할 수 있도록하는 것은 그래서 나는 것을 추가 할 수있다 나에게 세금을 알려주십시오 완벽한 솔루션을 DEMO

입니다 스크립트에

참고 변경 내용은 html과 스크립트에 적용됩니다.

새 책을 추가하려면 지금 가지고있는 구조를 따르십시오. -

<div id="content"> 
    <table border="1"> 
     <tr> 
      <th>Book Cover</th> 
      <th>Title & Author</th> 
      <th>Price</th> 
      <th>Quantity</th> 
     </tr> 
     <tr> 
      <td class="image"> 
       <a href="cover2.html" onClick="return openNewWindow(this.href, 475, 725);"><img alt="Book Cover" src="images/covers/2artfielding.png" /></a> 
      </td> 
      <td class="title"> 
       <p class="table"><b>The Art of Fielding</b></p> 
       <p class="table"><i>by Chad Harbach</i></p> 
      </td> 
      <td class="price"> 
       <p class="bookprice" id="artprice">$28.99</p> 
      </td> 
      <td class="quantity"> 
       <input type="text" id="artquantity" value="1" /><br /> 
      </td> 
     </tr> 
     <tr> 
      <td class="image"><a href="cover5.html" onClick="return openNewWindow(this.href,475, 725);"><img alt="Book Cover" src="images/covers/18thelovers.png" /></a></td> 

      <td class="title"> 
       <p class="table"><b>The Lover's Dictionary</b></p> 
       <p class="table"><i>by David Levithan</i></p> 
      </td> 
      <td class="price"> 
       <p class="bookprice" id="loverprice">$19.95</p> 
      </td> 

      <td class="quantity"> 
       <input type="text" id="loverquantity" value="1" /><br /> 
      </td> 
     </tr> 

     <tr> 
      <td class="image"><a href="cover4.html)" onClick="return openNewWindow(this.href,475, 725);"><img alt="Book Cover" src="images/covers/11nightcircus.png" /></a></td> 
      <td class="title"> 
       <p class="table"><b>The Night Circus</b></p> 

       <p class="table"><i>by Erin Morgenstern</i></p> 
      </td> 
      <td class="price"> 
       <p class="bookprice" id="nightprice">$32.00</p> 
      </td> 
      <td class="quantity"> 
       <input type="text" id="nightquantity" value="1" /><br /> 
      </td> 

     </tr> 
     </table> 
     <br /> 
     <p class="totals" id="subtotal">Sub-total:</p> 
     <p class="totals" id="taxes">Taxes:</p> 
     <p class="totals" id="grandtotal">Grand-total:</p> 
</div> 

자바 스크립트가 귀하의 js 파일이 대체

var popimage = 1; 
function openNewWindow(href, width, height) { 
    // The popup name will be popup1, popup2, popup3, …. etc. 
    // Notice how we've concatenated the 'width' and 'height' parameters. 
    var w = window.open(href, "popup"+popimage, 
    "width=" + width + ",height=" + height + 
    ",top=10,left=10,screenX=10,screenY=10,resizable=1,scrollbars=1,menubar=0"); 
    popimage++; 
    return w?false:true; 
} 

var books = {} 
window.onload=function() { 
    var inputs = document.getElementsByTagName('input'); 
    for (var i=0;i<inputs.length;i++) { 
    if (inputs[i].type=="text" && inputs[i].id.indexOf('quantity')!=-1) { 
     var name = inputs[i].id.replace('quantity',''); 
     books[name] = parseFloat(document.getElementById(name+'price').innerHTML.replace('$','')) 
     inputs[i].onkeyup=function() { 
     var total = 0; 
     for (var book in books) { 
      var q = document.getElementById(book+"quantity").value; 
      total += (isNaN(q) || q=="")?0:parseInt(q)*books[book] 
     } 
     document.getElementById('subtotal').innerHTML="Sub-total: $"+total.toFixed(2);  
     document.getElementById('taxes').innerHTML="Taxes: $"+(total*.06).toFixed(2);  
     document.getElementById('grandtotal').innerHTML="Grand-total: $"+(total*1.06).toFixed(2);  
     } 
    } 
    } 
} 
+0

와우 감사합니다. 훌륭한 작품 : D –

0

귀하의 문제는 문서가 완전히로드하기 전에 변수의 값을 설정하려고하는 것입니다 :

여기 내 자바 스크립트입니다. 시도 :

var artprice = 28.99, artquantity, arttotal, 
    loverprice = 19.95, loverquantity, lovertotal, 
    nightprice = 32.00, ightquantity, nighttotal; 


document.onload = function() { 
    artquantity = document.getElementById("artquantity").value; 
    arttotal = (artprice * artquantity).toFixed(2) - 0; 


    loverquantity = document.getElementById("loverquantity").value; 
    lovertotal = (loverprice * loverquantity).toFixed(2) - 0; 


    nightquantity = document.getElementById("nightquantity").value; 
    nighttotal = (nightprice * nightquantity).toFixed(2) - 0; 
} 

또는 더 나은, 여기에 미래가

var loadFuncs = []; 

function addToload = function(func) { 
    loadFuncs.push(func) 
} 

function setUpPriceFields() { 
    artquantity = document.getElementById("artquantity").value; 
    arttotal = (artprice * artquantity).toFixed(2) - 0; 


    loverquantity = document.getElementById("loverquantity").value; 
    lovertotal = (loverprice * loverquantity).toFixed(2) - 0; 


    nightquantity = document.getElementById("nightquantity").value; 
    nighttotal = (nightprice * nightquantity).toFixed(2) - 0; 
} 

addToLoad(setUpPriceFields); 

document.onload = function() { 
    var func; 
    while(func = loadFuncs.shift()) { 
     func(); 
    } 
} 
+0

글쎄, 나는 모든 것을 알아 냈다고 생각하지만, 여전히 "96.9 달러"대신에 "$ 96.90"이라는 돈을 항상 두 자릿수로 표시하는 방법을 궁금해합니다. –

+0

거기에 속하지 않는 -0에 의한 것 – mplungjan