[英]Why is my for loop in JavaScript causing my webpage to freeze?
I am currently working on a project, and I need to make an alert box pop up if the user inputs a value that is not a number. 我当前正在从事一个项目,如果用户输入的值不是数字,则需要弹出一个警告框。 I created a for loop to do this and it works, however whenever I type in something that is not a number, my webpage freezes and I have to close it. 我创建了一个for循环来执行此操作,并且该循环有效,但是,每当键入非数字的内容时,我的网页就会冻结,因此必须将其关闭。 I can't figure out what I am doing wrong. 我不知道自己在做什么错。 Here's the code that I have. 这是我的代码。
var Y=1
for(var i = 0; i < Input.length; i++)
{
if(isNaN(Input.charAt(i)))
{
alert("Parameter is Not A Number");
Y = 0;
}
}
If you are using HTML5 you can limit user input to numeric values by simply specifying 如果您使用的是HTML5,则只需指定以下内容即可将用户输入限制为数字值
<input type="number">
Try this if you want to stick to alert box and stuff 如果您要坚持提醒框和其他内容,请尝试此操作
<input type="input" id="edit1"/>
Js s
$(document).ready(function(){
$('[id^=edit]').keypress(validateNumber);
});
var Y=1
function validateNumber(event) {
var key = window.event ? event.keyCode : event.which;
if ( key < 48 || key > 57 ) {
alert("Parameter is Not A Number");
Y = 0;
}
else return true;
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.