Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
外部JavaScript文件中的变量,因此无需再次声明。正如其他人所说,在JavaScript文件中创建一个函数,并使用参数调用它。下面是一个示例(请注意,我使用div元素来显示结果):_Javascript_Variables_If Statement_Events - Fatal编程技术网

外部JavaScript文件中的变量,因此无需再次声明。正如其他人所说,在JavaScript文件中创建一个函数,并使用参数调用它。下面是一个示例(请注意,我使用div元素来显示结果):

外部JavaScript文件中的变量,因此无需再次声明。正如其他人所说,在JavaScript文件中创建一个函数,并使用参数调用它。下面是一个示例(请注意,我使用div元素来显示结果):,javascript,variables,if-statement,events,Javascript,Variables,If Statement,Events,(外部JS文件) var价格=5; var数量=0; var Ldisconcentrate=0.05; var hdiscountrate=0.1; var合计=0; var oldtotal=0; 函数重新计算(差异) { var resultem=null; 尝试 { 如果(数量==0&&diff=10) { 折扣总额=总额*ldiscontrate; resultElem.innerHTML=“您的折扣总额为”+oldtoal+“折扣后的总额”+total; } 否则,如果(oldtot

(外部JS文件)

var价格=5;
var数量=0;
var Ldisconcentrate=0.05;
var hdiscountrate=0.1;
var合计=0;
var oldtotal=0;
函数重新计算(差异)
{
var resultem=null;
尝试
{
如果(数量==0&&diff=10)
{
折扣总额=总额*ldiscontrate;
resultElem.innerHTML=“您的折扣总额为”+oldtoal+“折扣后的总额”+total;
}
否则,如果(oldtotal>=15)
{
总计=旧总计*hdiscountrate;
resultElem.innerHTML=“您的折扣总额为”+oldtoal+“折扣后的总额”+total;
}
其他的
{
总数=旧总数;
resultElem.innerHTML=“您没有折扣,总折扣为“+total+”$”;
}
}
}
捕获(e)
{
警报(“重新计算错误:+e.Message”);
}
最后
{
}
返回;
}
(HTML文件)


样本答案
添加一项
删除一项


您已经在使用事件。但是我建议您使用函数。
var quantity+1
是一个语法错误,除非您告诉它,否则您的代码只能运行一次。你知道了吗?您可能还希望检查开发人员工具中的控制台是否存在错误,因为您的代码正在生成一些错误。是一个很好的学习资源。需要执行语句(包括if语句)。当某个地方使用的变量发生变化时,它不会自动执行。在执行分配给
数量
后,需要重新执行。我建议把它放在一个函数中,这样你就可以很容易地调用它,并且不重复整个代码。在你的代码中,在按下一个代码之后,按钮就绝望了。这是因为你使用的是代码>文档(写)(< /代码>),我已经修复了它,检查。@ AhmedYasir,检查答案,如果有帮助的话,考虑接受。干杯
<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>
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();
}
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();
});
<!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>
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>
<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>