[英]Javascript function not working when called in a form tag
This is my HTML 这是我的HTML
<form>
<p>
Enter the total stock value: <input id="total" type="number"onkeyup="calculate()">
</p>
<p>
Enter the agreed commision rate:<input id="commision" type="number">
</p>
<p>
<input id="calculate" type="number" readonly="readonly" placeholder="Result">
</p>
</form>
and this is my Javascript 这是我的Javascript
calculate = function() {
var total = document.getElementById('total').value;
var commision = document.getElementById('commision').value;
document.getElementById('calculate').value = parseFloat(total) / 100 * parseFloat(commision);
}
$(document).keyup(function(e) {
if(e.keyCode== 27) {
$("form")[0].reset();
}
});
Now, if my form is not wrapped in a form tag my Calculate function works perfectly, but when it's in a form tag I'll get an error in the console saying "Uncaught TypeError: calculate is not a function". 现在,如果我的表单没有包装在表单标签中,则我的Calculate函数可以正常工作,但是当它在表单标签中时,我会在控制台中收到一条错误消息,提示“未捕获的TypeError:calculate不是函数”。 I tried without the form tag, but then the reset() function is not working..
我试过没有form标签,但是reset()函数不起作用。
Does anyone have any idea how to solve this issue? 有谁知道如何解决这个问题?
那是因为你有ID计算元素,元素的IDS被复制的形式,对象的属性(也窗口属性),所以你的情况calculate
已经覆盖了形式的范围内使用时,指的是DOM元素功能
<input id="txtcalculate" type="number" readonly="readonly" placeholder="Result">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.