注册 登录
编程论坛 JavaScript论坛

求助---变量被重置问题---求助

IQ998 发布于 2018-05-02 09:51, 1978 次点击
以下这段代码 变量 var t=0;老是被重置。请各位老师帮我看看为什么。
==================================================================================
<html>
<head>
    <meta charset="UTF-8">
    <div id="div" style="width:10000px; height:50px">
        <h1> 测试 </h1>
    </div>

   <script>
        <!--var d = document.getElementById("div");
        <!--d.style.backgroundColor = "blue";
    </script>
</head>

<form id="f1">

    匹配字符:
    <input type="text" id="RR1" style="width:500px;height:30px" value="41.22.22.22.13"><hr>
    <!录入数据:<input type="text" id="RR" onchange="myFunction()" onkeypress="myFunction()">



    录入数据:<input type="text" id="RR" style="width:500px;height:30px" onkeypress="myFunction()" > <br>
    <button id="ftt" onclick="myFunction()">保存</button>

</form>

<body>
    <script>
        var oTxt = document.getElementById('RR').focus(); <!--锁定焦点
        var all1 = new Array();
       var t = 0;
        console.log(t)
        function myFunction() {
            var input1=document.getElementById('RR').value;
            var input2=document.getElementById('RR1').value;
            var patt1 = new RegExp(input2);
            var x = "";
            if (event.keyCode == 13) {
                if ((patt1.test(input1) == true) && (undefined != input1) && ("" != input1)) {
                    x = "YES"

                    all1[t] = input1;
                    ++t;
                    document.getElementById('RR').value = ""
                    document.getElementById('DF1').innerHTML = all1;
                    console.log(t)
                    console.log(all1)
                } else {
                    x = "NO"
                }
            }

            document.getElementById("DF").innerHTML = x;
        }

        
    </script>



    判断:
    <output id="DF"></output>
    <hr>累计:
    <output id="DF1"></output>
    <hr>计数:
    <output id="DF2"></output>
    <hr>




</body>

</html>

2 回复
#2
IQ9982018-05-02 09:52
DDDDDDDDDDDDDD
#3
leeqihero2018-05-11 20:38
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div id="div" style="width:10000px; height:50px">
    <h1> 测试 </h1>
</div>
匹配字符:
<input type="text" id="RR1" style="width:500px;height:30px" value="41.22"><hr>

录入数据:<input type="text" id="RR" style="width:500px;height:30px"> <br>
<input type=button id="ftt" onclick="myFunction()" value=保存 /><br>

判断:
<output id="DF"></output>
<hr>累计:
<output id="DF1"></output>
<hr>计数:
<output id="DF2"></output>
<hr>
</body>
<script>
$=function(a){return document.getElementById(a);};
d=$("div");
d.style.backgroundColor = "blue";
$('RR').focus();
var list =[];
function myFunction() {
    var input1=$('RR').value;
    var input2=$('RR1').value;
    var patt1 = new RegExp(input2);
    var x =true;
    if (patt1.test(input1) && (undefined != input1) && ("" != input1)) {
        list.push(input1);
        $('RR').value = ""
        $('DF1').innerHTML = list.join('■');
        $('DF2').innerHTML = list.length;
    } else {
        x = false;
    }
    $("DF").innerHTML = x;
}   
</script>
</html>
1