简体   繁体   English

增量jQuery val()无法正常工作

[英]increment jQuery val() not working

I have a function myTimer() which makes a counter. 我有一个函数myTimer()来做一个计数器。 I want to increment input value when user clicks next button and repeat counter. 我想在用户单击下一步按钮并重复计数器时增加输入值。 The problem is every time it puts a new value of (rem) in input but I want to increase it by new rem. 问题是每次它将(rem)的新值输入时,但我想通过新的rem增加它。 dur is a variable declared in myTimer() it's user entered to make count, and seconds the variable counted, it's declared in myTimer() too. dur是在myTimer()中声明的变量,用户输入该变量进行计数,然后数秒该变量,也在myTimer()中声明。

  $(document).ready(function(){
       var rem= 0;            
        $("#next").click(function(){
            rem+= dur-seconds-1;
           $("input#time").val(rem);

             myTimer();
       });
});

You have declared JS variable var rem= 0; 您已声明JS变量var rem= 0; inside document.ready. 内部document.ready。

Declare it globally, before $(document).ready() $(document).ready()之前全局声明它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM