模拟键入键并减少2的值

时间:2016-03-22 11:00:56

标签: javascript reduce simulate

我有一些用于输入input元素的javascript代码。它输入3。这是我的代码:

    var txt = "3";
var timeOut;
var txtLen = txt.length;
var char = 0;
var tb = $("#act_bub input").attr("value", "|");
(function typeIt() {
    var humanize = Math.round(Math.random() * (200 - 30)) + 30;
    timeOut = setTimeout(function () {
        char++;
        var type = txt.substring(0, char);
        tb.attr("value", type + '|');
        typeIt();

        if (char == txtLen) {
            tb.attr("value", tb.attr("value").slice(0, -1))
            clearTimeout(timeOut);
        }

    }, humanize);
}());

这是我的HTML代码:

<div id="test">
<input type="text" value="6">
</div>

那么如何才能使这段代码c 检测输入元素的值并减少其中的2 并输入?例如在我的输入值为6时,代码检查值然后放4?

1 个答案:

答案 0 :(得分:0)

Here is my html code :
<div id="test">
<input type="number" id="num" value="6">
</div>
Your number is:
<div id='b'></div>

<script>
  var txt = document.getElementById('num').value-2;
  document.getElementById('b').innerHTML=txt;
  var timeOut;
  var txtLen = txt.length;
  var char = 0;
  var tb = $("#act_bub input").attr("value", "|");
  function typeIt() {
    var humanize = Math.round(Math.random() * (170)) + 30;
    timeOut = setTimeout(function () {
        char++;
        var type = txt.substring(0, char);
        tb.attr("value", type + '|');
        typeIt();

        if (char == txtLen) {
            tb.attr("value", tb.attr("value").slice(0, -1))
            clearTimeout(timeOut);
        }

    }, humanize);
  }
  </script>

使用document.getElementById('id').value查找输入值。