2017-07-18 41 views
0

我做了一个名为total的变量和一个if语句,用于检查总数是否大于或等于10,然后写入一些文本但if语句不刷新当我更改变量总有人告诉我我需要使用的事件在这种情况下,但我不知道你需要怎么if语句刷新javascript

var price = 5; 
 

 
var quantity = 0; 
 

 
var ldiscountrate = 0.05; 
 

 
var hdiscountrate = 0.1; 
 

 
var total = 0; 
 

 
var oldtotal = price * quantity; 
 

 
if (oldtotal >= 10) { 
 
    discounttotal = total * ldiscountrate; 
 
    document.write("you have a discount total was " + oldtotal + "total after discount " + total); 
 
} 
 

 
else if (oldtotal >= 15) { 
 
    total = oldtotal * hdiscountrate; 
 
    document.write("you have a discount total was " + oldtotal + "total after discount " + total); 
 
} 
 

 
else { 
 

 
    total = oldtotal 
 
    document.write("you don't have a discount the total is " + total + "$"); 
 

 
}
<!DOCTYPE html> 
 
<!-- 
 
To change this license header, choose License Headers in Project Properties. 
 
To change this template file, choose Tools | Templates 
 
and open the template in the editor. 
 
--> 
 
<html> 
 
    <head> 
 
     <title>Shopping</title> 
 
     <script type="text/javascript" src="JavaScript.js"></script> 
 
    </head> 
 
    <body> 
 
     
 
     <button type="button" onclick="var quantity+1">Add one item</button> 
 
     <button type="button" onclick="var quantity-1">Remove one item</button> 
 
     
 
    </body> 
 
</html>

+0

你已经事件。我建议你使用功能。 – Bergi

+0

'var quantity + 1'是一个语法错误 – Bergi

+1

您的代码只会运行一次,除非您另有说明。您是否了解了[函数](https://www.w3schools.com/js/js_functions.asp)?您可能还需要检查开发人员工具中的控制台是否存在错误,因为您的代码正在生成一些错误。 [freeCodeCamp](https://freecodecamp.com)是一个很好的学习资源。 – Gavin

回答

0

做一个函数并调用上onclick事件与更新值按钮。事情是这样的:

var price = 5; 
 

 
var quantity = 0; 
 

 
var ldiscountrate = 0.05; 
 

 
var hdiscountrate = 0.1; 
 

 
var total = 0; 
 

 
var oldtotal = price * quantity; 
 

 

 
function updateQuantity(increasedQuantity){ 
 
    quantity = increasedQuantity; 
 
    ldiscountrate = 0.05; 
 
    hdiscountrate = 0.1; 
 
    total = 0; 
 
    oldtotal = price * quantity; 
 

 
    if (oldtotal >= 10) { 
 
     discounttotal = total * ldiscountrate; 
 
     document.getElementById('result').innerHTML = "you have a discount total was " + oldtotal + "total after discount " + total; 
 
    } 
 

 
    else if (oldtotal >= 15) { 
 
     total = oldtotal * hdiscountrate; 
 
     document.getElementById('result').innerHTML = "you have a discount total was " + oldtotal + "total after discount " + total; 
 
    } 
 

 
    else { 
 

 
     total = oldtotal; 
 
     document.getElementById('result').innerHTML = "you don't have a discount the total is " + total + "$"; 
 

 
    } 
 
}
<!DOCTYPE html> 
 
<!-- 
 
To change this license header, choose License Headers in Project Properties. 
 
To change this template file, choose Tools | Templates 
 
and open the template in the editor. 
 
--> 
 
<html> 
 
    <head> 
 
     <title>Shopping</title> 
 
     <script type="text/javascript" src="JavaScript.js"></script> 
 
    </head> 
 
    <body> 
 
     
 
     <button type="button" onclick="updateQuantity(quantity+1)">Add one item</button> 
 
     <button type="button" onclick="updateQuantity(quantity-1)">Remove one item</button> 
 
     <div id="result"></div> 
 
    </body> 
 
</html>

不知道你想要显示它,但document.write不工作得非常好,就必须改变这种状况。

+0

在你的代码中,当你按下其中一个 –

+0

后,按钮正在失望,这是因为你正在使用'document.write()',现在我已经修复了它,请检查。 – Dij

+0

@AhmedYasir,检查答案,考虑接受它是否有帮助。干杯! – Dij

0

这会帮助你,不要忘记包含jquery库。 您可以根据需要更改您的计算, 我按照我的理解完成了。

<div > 
    <button type="button" onclick="changequantity('add');">Add one item</button> 
    <button type="button" onclick="changequantity('remove');">Remove one item</button> 
    <input type="hidden" id="quantity" value="0"> 
    <input type="hidden" id="total" value="0"> 
</div> 

<script> 
function changequantity(dothis){ 
    var price = 5; 
    var ldiscountrate = 0.05; 
    var hdiscountrate = 0.1; 
    var quantity = $('#quantity').val(); 
    var total = $('#total').val(); 
    if(dothis=="add"){ 
     quantity++; 
    } 
    if(dothis=="remove"){ 
     quantity--; 
    } 

    var oldtotal = price * quantity; 

    if (oldtotal >= 10) { 
     total = oldtotal - (oldtotal * ldiscountrate); 
     console.log("you have a discount total was " + oldtotal + " total after discount " + total); 
    }else if (oldtotal >= 15) { 
     total = oldtotal * hdiscountrate; 
     console.log("you have a discount total was " + oldtotal + " total after discount " + total); 
    }else{ 
     total = oldtotal; 
     console.log("you don't have a discount the total is " + total + "$"); 
    } 
    $('#quantity').val(quantity); 
    $('#total').val(total); 
} 
</script> 
0

首先,而不是寻找按钮的onclick处理您的quantity变量,你必须引用的函数。原因是onclick需要一个函数,也称为事件处理函数或事件回调函数。

我们可以创建一个addItem()函数,增加quantity和另一个removeItem()减少它。然后,我们可以让每个函数调用updateQuantity以使所有内容保持最新。请记住,我们应该努力保持我们的职能精益,并遵循单一责任原则。所以,你的代码看起来是这样的(我把它换成了document.writeconsole.log为清楚起见,将解释如何提高进一步下降):

var price = 5; 
var quantity = 0; 
var lDiscountRate = 0.05; 
var hDiscountRate = 0.1; 
var total = 0; 

function updateQuantity(){ 
    var oldTotal = price * quantity; 
    var discountTotal = total * lDiscountRate; 

    if (oldTotal >= 10) { 
     console.log("you have a discount total was " + oldTotal + " total after discount " + total); 
    } else if (oldTotal >= 15) { 
     total = oldTotal * hDiscountRate; 
     console.log("you have a discount total was " + oldTotal + " total after discount " + total); 
    } else { 
     total = oldTotal; 
     console.log("you don't have a discount the total is " + total + "$"); 
    } 
} 

function addItem() { 
    quantity++; 
    updateQuantity(); 
} 

function removeItem() { 
    quantity--; 
    updateQuantity(); 
} 

在这里,我们已经做了一些重要的东西。首先,我们将您的处理逻辑转移到自己的updateQuantity函数中,每次添加或删除项目时都会运行它。这确保了数据将像您所描述的那样保持“清爽”。另一个重要的事情是在功能内部移动oldTotal,以便在每次调用时都得到更新。我还声明discountTotal你没有声明,并没有在你的代码中使用。

现在我们只需要将我们的处理函数addItemremoveItem绑定到DOM(您的按钮)。我们可以将对函数的引用传递给onclick处理程序,或者我们可以使用事件侦听器,这是一种更好的做法。一个事件监听器将监听一个规定的事件(在这个例子中为'click',然后执行一个函数),我们需要为每个按钮添加id,然后在我们的代码中查找它们,我们将使用document.querySelector()来查找每个按钮和存储在一个变量,然后使用addEventListener方法,所有html元素必须添加侦听器。

最后,我还将添加一个空的<div>在哪里将打印我们的消息。使用document.write,而不是写入html元素。为此,我们还使用document.querySelector()来查找我们想要显示结果的元素,然后将您的字符串添加到innerHTML属性。

var messageDiv = document.querySelector('#results'); 

var price = 5; 
var quantity = 0; 
var lDiscountRate = 0.05; 
var hDiscountRate = 0.1; 
var total = 0; 

function updateQuantity(){ 
    var oldTotal = price * quantity; 

    if (oldTotal >= 10) { 
     var discountTotal = total * lDiscountRate; 

     messageDiv.innerHTML = "you have a discount total was " + oldTotal + " total after discount " + total; 
    } else if (oldTotal >= 15) { 
     total = oldTotal * hDiscountRate; 
     messageDiv.innerHTML = "you have a discount total was " + oldTotal + " total after discount " + total; 
    } else { 
     total = oldTotal; 
     messageDiv.innerHTML = "you don't have a discount the total is " + total + "$"; 
    } 
} 

function addItem() { 
    quantity++; 
    updateQuantity(); 
} 

function removeItem() { 
    quantity--; 
    updateQuantity(); 
} 

var addButton = document.querySelector('#addButton'); 
var removeButton = document.querySelector('#removeButton'); 

addButton.addEventListener('click', function() { 
    addItem(); 
}); 

removeButton.addEventListener('click', function() { 
    removeItem(); 
}); 

最后,我们更新HTML位包括id每个按钮和div打印我们的结果。请注意我们不再需要使用onclick属性。

<!DOCTYPE html> 
<!-- 
To change this license header, choose License Headers in Project Properties. 
To change this template file, choose Tools | Templates 
and open the template in the editor. 
--> 
<html> 
    <head> 
     <title>Shopping</title> 
     <script type="text/javascript" src="JavaScript.js"></script> 
    </head> 
    <body> 
     <div id="results"></div> 
     <button type="button" id="addButton">Add one item</button> 
     <button type="button" id="removeButton">Remove one item</button> 
    </body> 
</html> 

这里有一个工作例子给你:http://jsbin.com/rejutudese/edit?html,js,output

0

您已经声明的外部JavaScript文件中的“量”变,所以你不需要再次声明。正如其他人所说的那样,在JavaScript文件中创建一个函数并用参数调用它。下面是一个示例(请注意,我用一个div元素来显示结果):*是*使用

(外部JS文件)

var price = 5; 
var quantity = 0; 
var ldiscountrate = 0.05; 
var hdiscountrate = 0.1; 
var total = 0; 
var oldtotal = 0; 

function recalculate(diff) 
{ 
    var resultElem = null; 

    try 
    { 
     if(quantity==0 && diff<=0) 
     { 
      //Ignore 
      return; 
     } 
     else 
     { 

      resultElem = document.getElementById("theResult"); 

      quantity+=diff; 
      oldtotal = price * quantity; 

      if (oldtotal >= 10) 
      { 
        discounttotal = total * ldiscountrate; 
       resultElem.innerHTML="you have a discount total was " + oldtotal + " total after discount " + total; 
      } 
      else if (oldtotal >= 15) 
      { 
        total = oldtotal * hdiscountrate; 
       resultElem.innerHTML="you have a discount total was " + oldtotal + " total after discount " + total; 
      } 
      else 
      { 
       total = oldtotal; 
       resultElem.innerHTML="you don't have a discount the total is " + total + "$"; 
      } 
     } 
    } 
    catch(e) 
    { 
     alert("recalculate Error: " + e.Message); 
    } 
    finally 
    { 

    } 

    return; 
} 

(HTML文件)

<html> 
<head> 
<title>Sample Answer</title> 
<script type="text/javascript" language="javascript" src="JavaScript.js"></script> 
</head> 
<body> 
    <button type="button" onclick="recalculate(1)" >Add one item</button> 
    <button type="button" onclick="recalculate(-1)" >Remove one item</button> 
    <hr/> 
    <div id="theResult"></div> 
</body> 
</html>