2017-02-21 7 views
0

JavaScriptを学ぶ練習をしています。私はプログラムを動的にする問題があります。ここでやりたいことは次のとおりです。静的関数を動的にする

2つの入力を作成して、ユーザーが新しい製品データを設定できるようにします。 ボタンを作成します。このボタンには、次のようなクリックイベントが割り当てられます。1入力からデータを取得する。 2保存したデータで新しい製品列を作成します。 すべての商品を削除し、商品を想起させる価格に基づいて合計価格を下げることができます。何の項目がない場合は、合計金額が0

HTMLのようになります。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
<title>Shoping</title> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
<link rel="stylesheet" type="text/css" href="css/style.css"> 
<script src="js/index.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
</head> 
    <body> 
    <div class="container"> 
    <header> 
    <h1 class="h1 text-center mb">Shoping<span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span></h1> 
</header> 
<table class="table mb"> 
<thead> 
    <tr> 
    <th>Products:</th> 
    <th>Price:</th> 
    <th>Quantity:</th> 
    <th>Total:</th> 
    <th> 
    </th> 
    </tr> 
    </thead> 
    <tbody id="soyTBody"> 
    <tr id="itemSubject"> 
     <th scope="row"><span id="productName">T-Shirts</span></th> 
     <td class="price1"> <span class="dolar">$</span><span id="product">15</span></td> 
     <td class="qty1"> 
     <form class="form-inline"> 
      <div class="form-group"> 
      <label for="inputQty">QTY</label> 
      <input type="text" id="qty" class="form-control mx-sm-3" maxlength="3" aria-describedby="qtyItem1"> 
      </div> 
     </form> 
     </td> 
     <td <span class="dolar">$</span><span id="total">0.00</span></td> 
    <td class="text-right"> 
      <button class="delate btn btn-danger" type="button" onclick="removeItem()" value="delate"> 
      <input id ="delateButton" class="delateItem" type="button" value="Delate"> 
     </button> 
     </td> 
    </tr> 
    <tr id="itemSubject2"> 
    <th scope="row"><span id="productName">Stickers</span></th> 
    <td class="price2"> <span class="dolar">$</span><span id="product2">2</span></td> 
    <td class="qty2"> 
    <form class="form-inline"> 
     <div class="form-group"> 
     <label for="inputQty">QTY</label> 
     <input type="text" id="qty2" class="form-control mx-sm-3" maxlength="3" aria-describedby="qtyItem2"> 
     </div> 
    </form> 
    </td> 
    <td <span class="dolar">$</span><span id="total2">0.00</span></td> 
    <td class="text-right"> 
     <button class="delate btn btn-danger" type="button" onclick="removeItem2()" value="delate"> 
     <input id ="delateButton" class="delateItem" type="button" value="Delate"> 
    </button> 
    </td> 
    </tr> 
<!-- <tr> 
    <th scope="row">Stickers</th> 
    <td class="price2">$1</td> 
    <td class="qty2"> 
    <form class="form-inline"> 
     <div class="form-group"> 
     <label for="inputPassword4">QTY</label> 
     <input type="text" id="qty2text" class="form-control mx-sm-3" aria-describedby="passwordHelpInline"> 
     </div> 
    </form> 
    </td> 
    <td class="total-2">$0.00</td> 
    <td class="text-right"> 
     <button class="delate btn btn-danger" type="button" value="delate"> 
     <span class="delateButton"><strong>Delate</strong></span> 
    </button> 
    </td> 
</tr> --> 
<!-- <tr> 
    <th scope="row">Flags</th> 
    <td class="price3">$2</td> 
    <td class="qty3"><form class="form-inline"> 
    <div class="form-group"> 
    <label for="inputPassword4">QTY</label> 
    <input type="text" id="qty3text" class="form-control mx-sm-3" aria-describedby="passwordHelpInline"> 
    </div> 
    </form> 
</td> 
    <td class="total3">$0.00</td> 
    <td class="text-right"> 
    <button class="delate btn btn-danger" type="button" value="delate"> 
     <span class="delateButton"><strong>Delate</strong></span> 
    </button> 
    </td> 
</tr> --> 
    </tbody> 
    </table> 
    <div class="row text-right"> 

    <button class="delate btn btn-success" onclick="getPrice();getPrice2();totalPrice2()" type="submit" value="calculatePriceButton" > 
    <input id ="calc-prices-button" class="calculator" type="button" value="Calculate Prices"> 
<!-- <span class="delateButton"><strong>Calculate Prices</strong></span> --> 
    </button> 
    </div> 
    <h2 class="text-center">Total Price: $<span id="totalPrice"></span></h2> 
    </div> 
</body> 
</html> 

JS:あなたのgetPriceについては

// qty 

function getQty() { 
var qty = document.querySelector("#qty").value; 
document.querySelector("#total").innerHTML = qty; 
return qty; 
} 

getQty(); 


// qty2 

function getQty2() { 
var qty2 = document.querySelector("#qty2").value; 
document.querySelector("#total2").innerHTML = qty2; 
return qty2; 
} 

    getQty(); 


// Price 

    function getPrice() { 
    var price = parseInt(document.querySelector("#product").innerHTML); 
    document.querySelector("#total").innerHTML = price * getQty(); 
    } 

    getPrice(); 

// Price 2 

    function getPrice2() { 
    var price2 = parseInt(document.querySelector("#product2").innerHTML); 
    document.querySelector("#total2").innerHTML = price2 * getQty2(); 
    } 

    getPrice2(); 

    // total Price 

    function totalPrice2() { 
    var total = parseInt(document.querySelector("#total").innerHTML); 
    var total2 = parseInt(document.querySelector("#total2").innerHTML); 
    document.querySelector("#totalPrice").innerHTML = total + total2; 
} 

    totalPrice2(); 





//Romove Item 

function removeItem() { 
    var remove = document.querySelector("#itemSubject").remove("itemSubject"); 
    // setTimeout(function() { alert("Your shopping cart is empty"); }, 1000); 
    } 

    removeItem(); 

    function removeItem2() { 
    var remove = document.querySelector("#itemSubject2").remove("itemSubject2"); 
    // setTimeout(function() { alert("Your shopping cart is empty"); }, 1000); 
    } 

    removeItem2(); 
+0

関連するコードを含めるように質問を編集してください。外部リンクが壊れて、この質問とその回答が将来の訪問者にとってあまり役に立たない。 –

+0

確かに、今すぐやります – randseed1724

+0

これはhttp://codereview.stackexchange.com/に適しているのでしょうか? – Jan

答えて

1

()とgetPrice2()関数は、我々はそれらを作ることができます次のように変更することによって動的:

var price = parseInt(document.querySelector("#product").innerHTML); 

var price2 = parseInt(document.querySelector("#product2").innerHTML);  

これらの行は静的な名前の要素を探しますが、すべての入力に共通のクラス名を付けてループする必要があります。そうすれば、静的な要素IDに頼る必要はありません。私たちは、各入力にクラスのquantityinput」を与えた場合たとえば、私たちは、次の操作を実行できます。

var elements = document.getElementsByClassName("quantityinput"); 
for(var i=0; i<elements.length; i++) { 

    // Select this element and get its quantity 
    var this_total = elements[i].value; 

    // Get the price for this element by selecting the price td, then its second child 
    var this_price = elements[i].parentElement.parentElement.parentElement.previousSibling.childNodes[2].value; 

    // Select the total label for this row 
    var this_total_label = elements[i].parentElement.parentElement.parentElement.childNodes[1]; 

    // Now update the total label for this row by multiplying the price and the quantity 
    this_total_label.value = this_total * this_price; 
} 

今、我々はすべての既存の行を反復処理するので、不足している特定の行を心配する必要はありません。同様のアプローチは、すべての行の合計価格を計算し、各行の合計価格をループして合計した後、総価格ラベルに合計を割り当てるときに使用できます。

関連する問題