jQuery表单提交验证

时间:2016-12-27 13:37:24

标签: jquery forms

创建jQuery函数以提交表单时,我需要先发送验证才能发送请求。这是我的代码:

$('#Form').submit(function(e){
    e.preventDefault();
    e.stopImmediatePropagation();
    var PhotoID = $('#PhotoID').val();
    // I will set the PhotoID to 0 just for testing.
    var PhotoID = '0';
    if (PhotoID == '0') {
        $('#GlobalError').removeClass('hidden');
    } else {
        return true;
        $.ajax({
            type: "POST",
            url: $(this).attr( 'action' ),
            data: $(this).serialize(),
            beforeSend: function(){
                $("#loader").fadeIn(2000);
            },
            success: function( response ) {
            }
        });
    }
});

PhotoID0不同时,我想提交表单并按照正常的页面更改来形成操作属性。我知道e.preventDefault()的反义词是return true;,但不起作用。该功能正在被正确触发。所以问题就在于表单提交处理。

1 个答案:

答案 0 :(得分:1)

使用$('#Form').submit(function(e){ var PhotoID = $('#PhotoID').val(); // I will set the PhotoID to 0 just for testing. var PhotoID = '0'; if (PhotoID == '0') { $('#GlobalError').removeClass('hidden'); return false; } else { $.ajax({ type: "POST", url: $(this).attr( 'action' ), data: $(this).serialize(), beforeSend: function(){ $("#loader").fadeIn(2000); }, success: function( response ) { } }); } }); 来避免表单提交

PhotoID

但它永远不会提交,因为您将{{1}}值设置为0

相关问题