Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/433.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验证Else If不起作用-If is,Else not发出警报_Javascript_Jquery_Html_Validation - Fatal编程技术网

JavaScript验证Else If不起作用-If is,Else not发出警报

JavaScript验证Else If不起作用-If is,Else not发出警报,javascript,jquery,html,validation,Javascript,Jquery,Html,Validation,所以我在做一个动态表,它接受从英里到公里表的输入值。我的HTML工作正常,用于显示表的函数工作正常,但是,当向其中添加验证以确保不能输入负整数时,for循环中的if语句工作正常,而else语句破坏了代码。我非常不明白为什么 以下是我的JS代码: for (var i = from; i <= to; i++) { tr = document.createElement("tr"); if (i <= -1) alert("Value must be p

所以我在做一个动态表,它接受从英里到公里表的输入值。我的HTML工作正常,用于显示表的函数工作正常,但是,当向其中添加验证以确保不能输入负整数时,for循环中的if语句工作正常,而else语句破坏了代码。我非常不明白为什么

以下是我的JS代码:

  for (var i = from; i <= to; i++) {
    tr = document.createElement("tr");
    if (i <= -1)
      alert("Value must be positive Integer");
    else if (i % 2 == 0)
      tr.setAttribute("class", "even");
    else
      tr.setAttribute("class", "odd");

对于(var i=from;i请尝试此操作

if (i <= -1){
  alert("Value must be positive Integer");
  return false;
}
else if (i % 2 == 0)
  tr.setAttribute("class", "even");
else
  tr.setAttribute("class", "odd");
if(i
if(from>-1&&to>=from){

对于(var i=from;i)是否可以创建一个“演示”问题的代码段或小提琴,以便我们可以更好地查看。我的意思是,添加必要的HTML将使它变得简单。循环有什么问题,为什么不只是
if(from<0).
错误输出是什么?if(我告诉你CSS可以在一个表中添加斑马条纹,这样我就可以完全删除该逻辑。你可以使用
break
来确保
的初始
后的代码可以运行。@Dionys很好的观察结果…谢谢你..我已经更新了代码谢谢!!这解决了我的问题!:)
if (i <= -1)
  alert("Value must be positive Integer");
  break;
else if (i % 2 == 0)
  tr.setAttribute("class", "even");
else
  tr.setAttribute("class", "odd");
if (from > -1 && to>=from) {
for (var i = from; i <= to; i++) {
    tr = document.createElement("tr");
    if (i % 2 == 0)
      tr.setAttribute("class", "even");
    else
      tr.setAttribute("class", "odd");
}
else{
alert("Value must be positive Integer");
}