HTML验证码提交验证不起作用

时间:2014-03-04 11:47:33

标签: javascript html

代码:

<html>
<body>

<script>
window.onload = function () {
    var do_not_drag = document.getElementsByClassName('no_select');
    for (var i = 0; i < do_not_drag.length; i++) {
        disableSelection(do_not_drag[i]);
    }
};

function disableSelection(element) {
    if (typeof element.onselectstart != 'undefined') {
        element.onselectstart = function () {
            return false;
        };
    } else if (typeof element.style.MozUserSelect != 'undefined') {
        element.style.MozUserSelect = 'none';
    } else {
        element.onmousedown = function () {
            return false;
        };
    }
}

function generateCaptcha() {
    var captchaForRead = document.getElementById('captchaForRead');
    var captchaForReadInnerHTML = Math.floor(Math.random() * 9000) + 1000
    captchaForRead.innerHTML = captchaForReadInnerHTML;
    captchaCorr = captchaForReadInnerHTML;
}

function verifyCaptcha(captchaInput) {
    if (captchaInput == captchaCorr) {
        document.write("Correct!")
    }
}
</script>

<form onsubmit="verifyCaptcha(this.form.captchaInput.value);">
    <p id="captchaForRead" class="no_select"></p>
    <input type="text" name="captchaInput">
    <input type="submit" value="submit">
</form>

<script>
    generateCaptcha();
</script>

</body>
</html> 

这不起作用。我能做什么?即使我插入了正确的验证码,也没有发生任何事情。

我该怎么办?我已经检查了javascript和html但仍然无法找到问题。

请帮助。

1 个答案:

答案 0 :(得分:0)

试试这个:

function verifyCaptcha(captchaInput){
    console.log(captchaInput);
    if (captchaInput==captchaCorr){
         console.log('Correct');
    }
}

<form onsubmit="verifyCaptcha(this.captchaInput.value);">
<p id="captchaForRead" class="no_select"></p>
<input type="text" name="captchaInput">
<input type="submit" value="submit">
</form>