提示只跳第一次 - javascript

时间:2016-02-07 20:48:04

标签: 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);
            };
        }

};

3 个答案:

答案 0 :(得分:0)

使用此

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

答案 1 :(得分:0)

当你使用document.write时,它会删除你的html文件中的所有内容。

在页面上显示一个元素以显示您需要的文本会更好。试试这个:

在按钮标记的正上方添加此div标记:

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

然后用你的文件替换你的document.write('应付总额'+付款)行:

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

这只会替换指定div中的内容,而不是像document.write一样编写整个页面。

答案 2 :(得分:0)

正如其他人所说,使用document.write是你的错误,因为你覆盖了所有的HTML。更一般地说,我鼓励你试验模块模式(以避免在窗口上保存一个函数),bootstrap(使你的界面看起来很好),错误处理(如果用户没有输入正确的输入),以及jQuery(简化DOM操作)。我为你的问题here整理了一个傻瓜。

HTML          

<head>
  <meta charset="UTF-8" />
  <script data-require="jquery@*" data-semver="2.1.4" src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <script data-require="bootstrap@3.3.6" data-semver="3.3.6" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link data-require="bootstrap-css@3.3.6" 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));
相关问题