gpt4 book ai didi

javascript - 简单的 sum javascript 函数在 IE 中不起作用

转载 作者:行者123 更新时间:2023-11-28 20:47:04 24 4
gpt4 key购买 nike

此代码在 Chrome 中有效,但在 IE 中无效。为什么??很简单,但我没有看到我的错误。

<script>

function calc() {

var summ = (parseFloat(q2.value) + parseFloat(q3.value) + parseFloat(q4.value)+ ( parseFloat(qn2.value) + parseFloat(qn3.value) + parseFloat(qn4.value) ) * 6 ) * 1.13;

summ = parseFloat(summ).toFixed(2);

document.getElementById('sum2').innerHTML = summ;

alert(summ);
}

</ script>

和html(删除所有文字,仅输入)

<form method="" action="">

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '00.00';}" value="00.00" maxlength="14" size="4" id="q2"/>

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '00.00';}" value="00.00" maxlength="14" size="4" id="q3"/>

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '00.00';}" value="00.00" maxlength="14" size="4" id="q4"/>

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '0';}" value="0" maxlength="14" size="4" id="qn2"/>

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '0';}" value="0" maxlength="14" size="4" id="qn3"/>

<input type="number" onClick="calc();" onChange="calc();" onblur="if (this.value == '') {this.value = '0';}" value="0" maxlength="14" size="4" id="qn4"/>


<p><span id="sum2" style="color: red; font-weight: bold;">00.00</span> the result</p>

<center><input type='button' onclick='calc()' value='Обновить'/></center>

</ html>

我做错了什么?我已经发现 IE 不喜欢 name="..."标签 并从代码中删除了所有这些标签。

最佳答案

查看控制台,它告诉你有错误

SCRIPT5009: 'q2' is undefined show, line 22 character 9

不要直接通过 id 引用元素,需要使用 document.getElementById()

你需要做的是

parseFloat(document.getElementById("q2").value) 

关于javascript - 简单的 sum javascript 函数在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13270730/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com