0

这段代码有什么问题?

 $(function() {
            $('#my_form').submit(function(e) {
                e.preventDefault();
                recaptcha_response_field = $('input#recaptcha_response_field').val();
                recaptcha_challenge_field = $('input#recaptcha_challenge_field').val();

                $.post('controller.php', {recaptcha_response_field : recaptcha_response_field, recaptcha_challenge_field : recaptcha_challenge_field}, function(data) {
                if(data == 0) {
                    alert('no');
                } else {
                    alert('submit')
                    $('#my_form').submit();
                }
            }); 
            })
        });

我在我的表格中使用 recaptcha。当用户正确输入验证码时,这些代码行将被执行

alert('submit')
$('#my_form').submit();

这么胖那么好。问题是表单无法提交。我认为这行代码再次执行

$('#my_form').submit(function(e) {

有什么建议么 ?

4

2 回答 2

1

您可以在使用所有验证后取消绑定它

$('#my_form').unbind('submit');

然后定期使用

$('#my_form').submit();
于 2013-02-06T13:09:25.570 回答
0

I haven't tested this out yet, but try this. You're right that the .submit function is being triggered again.

$(function() {
    $('#my_form').submit(function(e) {
        'use strict';

        var recaptcha_response_field = $('input#recaptcha_response_field').val(),
            recaptcha_challenge_field = $('input#recaptcha_challenge_field').val(),
            submitForm = true;

        $.post('controller.php', {
                recaptcha_response_field : recaptcha_response_field,
                recaptcha_challenge_field : recaptcha_challenge_field
            }, function(data) {
                if(data == 0) {
                    submitForm = false;
                }
        }); 

        if(!submitForm) {
            e.preventDefault();
        }
    })
});
于 2012-11-23T10:08:37.833 回答