我的问题是text3在我的代码中未定义:

t += text2 + "Case #" + i + ":" + "<br>" + text3 + "<br>";


但它在这里:

$('#pass').keyup(function (e) {
    var strong = new RegExp("^(?=.{11,})(((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[a-z])(?=.*\\W))|((?=.*[a-z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var normal = new RegExp("^(?=.{4,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[a-z])(?=.*\\W))|((?=.*[0-9])(?=.*\\W))).*$", "g");
    if (strong.test($(this).val())) {
        text3 = "strong";
    } else if (normal.test($(this).val())) {
        text3 = "normal";
    } else {
        text3 = "weak";
    }
    return true;
});


这是我所有的代码:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
<p><input placeholder="number of tests" type="text" name="numbers" id="x"/></p>
<div id="passdiv"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">

    $('#x').keyup(function (e) {

        var i;
        var text2 = '';
        var t = "";
        var x = document.getElementById("x").value;
        for (i = 1; i <= x; i++) {
            text2 = '<p><input placeholder="test NO. ' + i + '" type="password" id="pass" /></p>';
            t += text2 + "Case #" + i + ":" + "<br>" + text3 + "<br>";
        }
        document.getElementById("passdiv").innerHTML = t;

        return true;
    });

    $('#pass').keyup(function (e) {
        var strong = new RegExp("^(?=.{11,})(((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[a-z])(?=.*\\W))|((?=.*[a-z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[0-9])(?=.*\\W))|((?=.*[A-Z])(?=.*[a-z])(?=.*[0-9]))).*$", "g");
        var normal = new RegExp("^(?=.{4,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))|((?=.*[a-z])(?=.*\\W))|((?=.*[0-9])(?=.*\\W))).*$", "g");
        if (strong.test($(this).val())) {
            text3 = "strong";
        } else if (normal.test($(this).val())) {
            text3 = "normal";
        } else {
            text3 = "weak";
        }
        return true;
    });

</script>
</body>
</html>


问题是什么?
请帮忙

最佳答案

好像在$('#x').keyup()之前调用了$('#pass').keyup()

关于javascript - 函数在我的代码中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26186636/

10-16 20:06