jQuery validate + form plugins导致随机失败(合并)

时间:2015-04-23 16:05:14

标签: javascript jquery ajax

问题:

有时用户报告说,他们最终会在http://phppos/.PHP-Point-Of-Sale-Prev/index.php/items/save/1上找到ajax提交而不是ajax提交(json数据显示给最终用户)。 (这假设是ajax)

我自己无法重现这一点,并且不确定它是如何发生的。

我正在寻找重现问题的方法。

您可以访问以下网址来尝试该表单:

http://demo.phppointofsalestaging.com/

点击登录

然后导航至:

http://demo.phppointofsalestaging.com/index.php/items/view/1/2

HTML

<form action="http://phppos/.PHP-Point-Of-Sale-Prev/index.php/items/save/1" method="post" accept-charset="utf-8" id="item_form" class="form-horizontal" enctype="multipart/form-data">


...
</form>

JS :(在页面底部的$(文件).ready中)

    $('#item_form').validate(
    {
        submitHandler:function(form)
        {
            $.post('http://phppos/.PHP-Point-Of-Sale-Prev/index.php/items/check_duplicate', {term: $('#name').val()},function(data) {
            {
                doItemSubmit(form);
            }} , "json")
             .error(function() 
             {
             });
        },
        errorClass: "text-danger",
        errorElement: "span",
            highlight:function(element, errorClass, validClass) 
            {       
                $(element).parents('.form-group').removeClass('has-success').addClass('has-error');
            },
            unhighlight: function(element, errorClass, validClass) 
            {
                $(element).parents('.form-group').removeClass('has-error').addClass('has-success');
            },
        rules:
        {
            "locations[1][quantity]":
            {
                number: true
            },
            "locations[1][reorder_level]":
            {
                number: true
            },
            "locations[1][cost_price]":
            {
                number: true
            },
            "locations[1][unit_price]":
            {
                number: true
            },          
            "locations[1][promo_price]":
            {
                number: true
            },          
            name:"required",
            category:"required",
            cost_price:
            {
                required:true,
                number:true
            },

            unit_price:
            {
                required:true,
                number:true
            },
            promo_price:
            {
                number: true
            },
            reorder_level:
            {
                number:true
            },
        },
        messages:
        {           
                "locations[1][quantity]":
                {
                    number: "This field must be a number"               
                },
                "locations[1][reorder_level]":
                {
                    number: "This field must be a number"               
                },
                "locations[1][cost_price]":
                {
                    number: "This field must be a number"               
                },
                "locations[1][unit_price]":
                {
                    number: "This field must be a number"               
                },          
                "locations[1][promo_price]":
                {
                    number: "This field must be a number"               
                },      
            name:"Item Name is a required field",
            category:"Category is a required field",
            cost_price:
            {
                required:"Cost Price is a required field",
                number:"Cost price must be a number"            
            },
            unit_price:
            {
                required:"Selling Price is a required field",
                number:"Unit price must be a number"            
            },
            promo_price:
            {
                number: "This field must be a number"           
            }
        }
    });
});

var submitting = false;

function doItemSubmit(form)
{
    if (submitting) return;
    submitting = true;
    $("#form").mask("Please wait...");
    $(form).ajaxSubmit({
    success:function(response)
    {
        $("#form").unmask();
        submitting = false;
        gritter(response.success ? "Success" +' #' + response.item_id : "Error" ,response.message,response.success ? 'gritter-item-success' : 'gritter-item-error',false,false);

        if(response.redirect==1 && response.success)
        { 
            if (response.sale_or_receiving == 'sale')
            {
                $.post('http://phppos/.PHP-Point-Of-Sale-Prev/index.php/sales/add', {item: response.item_id}, function()
                {
                    window.location.href = 'http://phppos/.PHP-Point-Of-Sale-Prev/index.php/sales'
                });
            }
            else
            {
                $.post('http://phppos/.PHP-Point-Of-Sale-Prev/index.php/receivings/add', {item: response.item_id}, function()
                {
                    window.location.href = 'http://phppos/.PHP-Point-Of-Sale-Prev/index.php/receivings'
                });
            }
        }
        else if(response.redirect==2 && response.success)
        {
            window.location.href = 'http://phppos/.PHP-Point-Of-Sale-Prev/index.php/items'
        }

      },
        dataType:'json'
   });
}

0 个答案:

没有答案