[英]Check and convert float Input Value in JavaScript
Check if a form input is float and round up if that's the case.检查表单输入是否是浮动的,如果是这种情况,则四舍五入。
I have a form and I want to check if the input is float and round up if that's the case.我有一个表格,我想检查输入是否是浮动的,如果是这种情况,则四舍五入。 Why is my code not working?
为什么我的代码不起作用?
<div class="col-md-12">
<input type="number" id="figure" class="form-control " placeholder="Assign Class Limit" min="1" max="100"/>
</div>
const figure = document.getElementById(figure");
figure.addEventListener("change", (event) =>
if(!isNaN(parseFloat(figure.value))) {
class_limit = Math.round(event.target.value)}
} else {
class_limit = event.target.value
}
} ) })
First off, your JavaScript is not enclosed in a script tag.首先,您的 JavaScript 没有包含在脚本标记中。 Secondly, your
getElementById
is not properly written, it's missing double quotes at the start of figure.其次,您的
getElementById
编写不正确,它在图的开头缺少双引号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.