【问题标题】:Form will not submit with captcha表单不会与验证码一起提交
【发布时间】:2015-10-13 16:45:57
【问题描述】:

我已将此作为我网站上需要验证码的表单的起点,但是在添加它后,它不会提交到 action.php 页面以移动用户。

我该怎么做才能提交?

http://jsfiddle.net/swade/M5rvh/

<form id="form" action="action.php">
    <input type="text" name="name" placeholder="Name" />

    <input type="submit" value="send" />
</form>

$(function(){

    var mathenticate = {
        bounds: {
            lower: 5,
            upper: 50
        },
        first: 0,
        second: 0,
        generate: function()
        {
            this.first = Math.floor(Math.random() * this.bounds.lower) + 1;
            this.second = Math.floor(Math.random() * this.bounds.upper) + 1;
        },
        show: function()
        {
            return this.first + ' + ' + this.second;
        },
        solve: function()
        {
            return this.first + this.second;
        }
    };
    mathenticate.generate();

    var $auth = $('<input type="text" name="auth" />');
    $auth
        .attr('placeholder', mathenticate.show())
        .insertAfter('input[name="name"]');

    $('#form').on('submit', function(e){
        e.preventDefault();
        if( $auth.val() != mathenticate.solve() )
        {
            alert('wrong answer!');
        }
    });

});

【问题讨论】:

    标签: javascript forms validation submit captcha


    【解决方案1】:

    默认操作是您一直阻止的表单提交。 只有在您的验证码不匹配时才阻止它。

    $('#form').on('submit', function(e){
        if( $auth.val() != mathenticate.solve() )
        {
            e.preventDefault();
            alert('wrong answer!');
        }
    });
    

    【讨论】:

    • 我认为这个名字会泄露出去。无论如何,您正在阻止使用此行的默认操作:e.preventDefault();
    • 我已经尝试删除该行,但即使验证码错误,它也会将用户移动到下一页
    • 您可能想阅读我回答中的代码块。只有在验证码不匹配时才会阻止默认操作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多