Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.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_Html_Css - Fatal编程技术网

javascript简单计算器不返回值

javascript简单计算器不返回值,javascript,html,css,Javascript,Html,Css,我正在使用一个简单的计算器表单,我正在尝试使用javascript来计算总数。我已经引用了源代码,ID是正确的,但是我会上传 单击“计算”按钮时,页面会将值刷新回正常值 有关HTML和JS,请参见下文 函数计算(){ "严格使用",; 总风险价值; var quantity=document.getElementsById('quantity')。值; var unitPrice=document.getElementById('price')。值; var theVat=document.g

我正在使用一个简单的计算器表单,我正在尝试使用javascript来计算总数。我已经引用了源代码,ID是正确的,但是我会上传

单击“计算”按钮时,页面会将值刷新回正常值

有关HTML和JS,请参见下文

函数计算(){
"严格使用",;
总风险价值;
var quantity=document.getElementsById('quantity')。值;
var unitPrice=document.getElementById('price')。值;
var theVat=document.getElementById('vat')。值;
var theDiscount=document.getElementById(“折扣”).value;
总价=数量*单价;
总计=总计+(总计*增值税/100)-(总计*折旧/100);
总计=总计。toFixed(2);
document.getElementById('total')。value=“£”+总计;
返回false;
}
函数initCal(){
"严格使用",;
document.getElementById('theCalculatorForm')。onsubmit=calculate;
}
window.onload=initCal

创新多媒体-简单计算器
创新多媒体-JavaScript简介
在此应用程序中,用户将输入所需的详细信息,应用程序将计算总成本

学生用函数计算器 使用此表格计算订单总额

量 单价 增值税(%) 折扣(%) 全部的
我们使用它通过两种方法(post/get)发送数据,因此当您单击 在按钮中,计算发送的所有值并刷新页面 例如,使用div不刷新网页

将表单标记替换为div 像这样

    <div id="theCalculatorForm">

          </div>
<!---- Your code after will be like this ------>
<div  id="theCalculatorForm">
        <fieldset>
          <p>Use this form to calculate the order total.</p>
          <div><label for="quantity">Quantity</label><input type="number" name="quantity" id="quantity" value="1" min="1" required></div>
          <div><label for="price">Price Per Unit</label><input type="number" name="price" id="price" value="1.00" required></div>
          <div><label for="vat">VAT (%)</label><input type="number" name="vat" id="vat" value="17" required disabled></div>
          <div><label for="discount">Discount (%)</label><input type="number" name="discount" id="discount" value="10" required disabled></div>
          <div><label for="total">Total</label><input type="text" name="total" id="total" value="0" required disabled></div>
          <div><input type="submit" value="Calculate" id="submitCal"></div>
        </fieldset>
      </div>

使用此表格计算订单总额

量 单价 增值税(%) 折扣(%) 全部的
在计算函数中,取一个事件参数并防止发生默认操作:

function calculate(event) {
    event.preventDefault();

    //other code will work 
}
然后在通话中使用事件:

<button onclick="calculate(e)">Calculate</button>
计算

它会工作的

你试过调试你的代码吗?你看到错误了吗?你说的“页面刷新”是什么意思?重新加载?
getElementsById
应该是单数,
getElementById
您应该查看一下以帮助调试。在这里,您的问题似乎是您没有阻止表单提交。请尝试在计算函数的顶部使用,而不是在最后使用
return false
,这样,如果在该点之后代码中出现错误,表单仍然不会实际提交。这将有助于您将来进行调试。