2016-02-07 95 views
0

我有一個非常容易的任務,但它不工作。 提示只是第一次跳起,現在什麼都沒有了。 我將不勝感激任何幫助!謝謝提示跳轉只有第一次 - javascript

作業: 當任何顧客購買超過1000件商品時,店主給出10%的折扣。 1.編寫一個函數,將購買的總項目(提示)和每個項目的價格(固定)作爲參數,並計算客戶應付的總金額。

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <script src = "8.js"></script> 
</head> 
<body> 
    <button onclick="check()">check</button> 
</body> 
</html> 





function check(){ 
var totalItem = parseInt(prompt('please enter total item purchased:')); 
var pricePerItem = 15.5; 

    if (totalItem > 1000){ 
     function topay(totalItem, pricePerItem){ 
     var toPay = (totalItem * pricePerItem) * 0.9; 
     console.log(toPay); 
     document.write ('total amount payable ' + toPay); 
      }; 
     } 

}; 
+1

'document.write'覆蓋整個文檔,包括你的元素和JavaScript – adeneo

+0

如何迅速 「跳」? – nnnnnn

回答

0

使用此

function topay(totalItem, pricePerItem){ 
     var toPay = (totalItem * pricePerItem) * 0.9; 
     console.log(toPay); 
     document.write ('total amount payable ' + toPay); 
      }; 
     topay(totalItem, pricePerItem); 
     } 
0

當您使用文件撰寫它將抹去一切從你的HTML文件。

讓頁面上的元素顯示所需的文本會更好。試試這個:

添加這個div標籤右側的按鈕,標籤上面:

<div id="divTotal"></div> 

然後更換您的文件撰寫( '總金額支付' + toPay)符合本:

document.getElementById("divTotal").innerHTML = 'total amount payable ' + toPay; 

這將只替換指定div中的內容,而不是像document.write一樣寫整個頁面。

0

正如其他人所說,使用document.write是你覆蓋所有ht​​ml的錯誤。更一般地說,我鼓勵你試驗模塊模式(避免在窗口中保存一個函數),引導(使你的接口看起來不錯),錯誤處理(如果用戶沒有輸入正確的輸入),以及jQuery(簡化DOM操作)。我爲你的問題編了一個plunkr here

HTML

<head> 
    <meta charset="UTF-8" /> 
    <script data-require="[email protected]*" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script> 
    <script data-require="[email protected]" data-semver="3.3.6" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
    <link data-require="[email protected]" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" /> 
    <script src="script.js"></script> 
</head> 

<body> 
    </br> 
    <div class="row"> 
    <div class="col-lg-6"> 
     <div class="input-group"> 
     <input type="text" id='totalItems' class="form-control" placeholder="Enter total amount of items purchased..."> 
     <span class="input-group-btn"> 
      <button class="btn btn-default btn-success" onclick='priceModule.processItemInput()' type="button">Get Total Price</button> 
      </span> 
     </div> 
     <!-- /input-group --> 
    </div> 
    <div class="col-lg-6"> 
     <h4> 
     Total Amount Due: 
     <span id='answerTotal'></span> 
     </h4> 
     <!-- /input-group --> 
    </div> 
    <!-- /.row --> 
</body> 

</html> 

的Javascript

// Declare a new module to avoid polluting the global namespace 
// http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad- 
// javascript-habits-part-1/) 
(function(priceModule, $) { 
    var pricePerItem = 15.5; 

    priceModule.processItemInput = function() { 

    // Get the current user input 
    var totalItems = $('#totalItems').val(); 
    var message = ''; 

    // Test for errors 
    var invalidInput = isInvalidInput(totalItems); 

    if (invalidInput) { 

     // Set the message as the correct error message 
     message = invalidInput; 
    } else { 

     // Set the message as total due 
     message = '$' + calculateTotal(totalItems, pricePerItem); 
    } 

    // Output the error message or total 
    $('#answerTotal').text(message); 

    // Return the focus to the input 
    $('#totalItems').focus(); 
    }; 

    // Takes a string and returns the appropriate error message 
    // or an empty string if no error. 
    var isInvalidInput = function(totalItems) { 
    message = ''; 
    if (isNaN(totalItems)) { 
     message = 'Please enter a valid number of items.' 
    } else if (parseInt(totalItems) < 0) { 
     message = 'Please enter a positive number of items.' 
    } else if (parseInt(totalItems) !== parseFloat(totalItems)) { 
     message = 'Please enter a whole number of items.' 
    } 
    return message; 
    }; 

    // Takes a total number of items (string) and pricePerItem (float) 
    // and returns a total amount due, after discount, rounded to 
    // the nearest cent. 
    var calculateTotal = function(totalItems, pricePerItem) { 
    var total = parseInt(totalItems) * pricePerItem; 

    // Apply discount if over 1000 
    if (total > 1000) { 
     total = total * .9; 
    } 

    // Round to the nearest cent. 
    return total.toFixed(2); 
    }; 
}(window.priceModule = window.priceModule || {}, jQuery));