Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery表单随验证一起提交_Jquery_Forms - Fatal编程技术网

jQuery表单随验证一起提交

jQuery表单随验证一起提交,jquery,forms,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';

当创建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 ) {
            }
        });
    }
});

PhotoID
0
不同时,我想提交表单,并按照常规页面更改表单操作属性。我知道
e.preventDefault()
的反面是
返回true但不起作用。该功能正在正确触发。因此,问题在于表单提交处理。

使用
返回false来避免表单提交

$('#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
值设置为0

它工作了。我标记它只是为了更好地理解代码。我将在将来为有相同问题的人更正代码。非常感谢。