html Var++未增加

lsmd5eda  于 2022-12-16  发布在  其他
关注(0)|答案(2)|浏览(98)

因此,即使在编写i++时,JavaScript的变量似乎也没有更新/递增(变量名只是i)每次调用它时,所谓调用是指按下HTML按钮时(顺便说一下,我之所以有i,是因为我把i推到一个数组中,以显示嵌套数组存储/调用的数字)。但同样,尽管出于某种原因,它似乎不起作用。我试图创建一个循环,但它并没有真正更新值,它只是发送更多的警报。有没有一种方法可以正确地更新或递增i的值,如果有,您介意简要地解释一下我的错误吗?非常感谢!:D

function onClick(){
    const checkerArr = [];
    var removedvals;

    let i = 0;

    var input = document.getElementById("input1").value;
    let checkj;

        function containsNoJ(str){
          const normieChars = /[abcedfghiklmnopqrstuvwxyz]{1}/i;
          return normieChars.test(str);
        }

        
        function containsJ(str){
          const containsj = /[j]{1}/i;
          return containsj.test(str);
        }

        function containsAnyLetters(str) {
            const specialChars = /[0-9`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]{1}/g;
            return specialChars.test(str);
        }


        if (containsNoJ(input)){
            checkj = "contains no j. ";

        }

        if (containsJ(input)){
            checkj = "contains j! ";

        }

        
        // if (containsJ(input) && containsNoJ(input)){
        //     checkj = "contains j!";
        // }

        if (containsAnyLetters(input)){
            checkj = "error: inconvertable characters typed ";

        }

        if (containsAnyLetters(input) && containsJ(input)){
            checkj = "error: inconvertable characters typed (j was found though! :D) ";

        }

        if (checkj == "" || checkj == null){
             checkj = "error: no value typed. ";

        }


    

       checkerArr.push([checkj, "Check Number ", i]);
    
        i++;
        checkj += "" + JSON.stringify(checkerArr);

        alert(checkj);

            
}
#input1{
  animation: fadeIn 10s;
  border-radius: 20px 20px 20px 20px;

  font-size: 20px;
  width: 200px;
  z-index: 400;
  position: relative;
  height: 100px;
  max-height: 1000px; 

  top: -10%;
  border:2px solid #9c1717
}

#button1 {
  border-top-width: 1px;
  transform: translateY(-4px);
  height:auto;
  width:auto;
  border-radius: 5px;
  box-shadow: 0 9px #999;
  position: relative;
  font-family:  'Trebuchet MS', sans-serif;
  top: -10%;
  width: 300px;

  height: 100px;
  animation: textGrow 2.5s; 
  animation: fadeIn 10s;
  font-size: 30px;
  background-color: #00ff55;
  transition: width 0.25s, height 0.25s, background-color 0.2s ease-out 1ms, font-size 0.5s ease-in 1ms, font-size 0.2s 
  ease-out 1ms;

}

#button1:hover {
  height:auto;
  width:auto;
  border-color: rgb(31, 117, 245) 5px;
  background-color: #04d326;
  cursor: pointer;
  font-size: 35px;
  width: 340px;
  height: 110px;
  
}

#button1:active {  
  box-shadow: 0 5px #777;
 transform: translateY(4px);
  background: rgb(47, 69, 141);
}

#no {
  left: 500px;
  border-top-width: 1px;
  transform: translateY(-4px);
 
  border-radius: 5px;
  box-shadow: 0 9px #999;
  position: relative;
  font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
  width: 300px;
  height: 100px;
  font-size: 30px;
  background-color: #94c7a5;
  transition: width 0.25s, height 0.25s, background-color 0.2s ease-out 1ms, font-size 0.5s ease-in 1ms, font-size 0.2s 
  ease-out 1ms;

}
<!DOCTYPE html>
<html>

<head>
    <link rel="stylesheet" type="text/css" href="j.css"/>
    <meta charset="utf-8" />
</head>

<body>

<input style=" position: absolute; top: 165px; display: flex; width: 250px;" placeholder="Type Your Input's value..." id="input1" step="0.1">
<button type="button" onclick="onClick();" id = "button1" style = "position: absolute; 
left: 300px;
 top: 170px;
 text-align: center;">Get Input value</button>

</body>
</html>
qnzebej0

qnzebej01#

i的作用域在onClick()函数内部,每次调用onClick()时都要重置它。请尝试将let i = 0;移到函数声明之外。

f2uvfpb9

f2uvfpb92#

应该将i变量移出函数,因为每次调用onClick()函数时,i都会被设置为0或无值
下面是你应该做的:

let i;
function onClick() {
//Your code here
...
}

相关问题