当ajax加载表单时,Jquery验证函数不起作用

时间:2018-05-31 07:42:50

标签: jquery ajax cs-cart

我在cs-cart checkout中使用Jquery验证方法。 当我使用cm-ajax类(css默认类)通过ajax请求转到此步骤时,validate函数不起作用,但如果我刷新页面则显示所有错误。

$("#step_two_billing_address").validate({
        rules: {
            "user_data[company]": "required",
            "user_data[company_gst]" :{
                regex : '[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[0-9]
               {1}[zZ]{1}[a-zA-Z0-9]{1}',
                maxlength : 15,
            },
            "user_data[b_zipcode]": "required",
            "user_data[b_phone]":{
                required :true,
                regex : '[6789][0-9]{9}',
                maxlength : 10,
            } ,
            "user_data[b_address]": "required",
            "user_data[b_city]": "required",
            "user_data[b_state]": "required",
        },

        messages: {

            "user_data[company]": "Please enter your company name",
            "user_data[b_zipcode]": "Please enter your pincode",
            "user_data[phone]": {
                required : "Please enter contact number",
                regex    : "Please enter a valid contact number",
                maxlength : "Please enter a valid contact number",
            },
            "user_data[b_address]": "Please enter billing address",
            "user_data[b_city]": "Please enter your city",
            "user_data[b_state]": "Please enter your state"
        },
        errorElement : 'span',
        errorPlacement: function(error, element) {
          element.addClass('error invalid');
          var label = $('#element').siblings('label');
          label.attr('class', 'active');
          var placement = $(element).data('error');
          if (placement) {
            $(placement).append(error)
          } else {
            error.insertAfter(element);
          }
        }
    });

刷新页面后的验证:

验证显示当我们转到结算地址部分throgh ajax时弹出窗口 enter image description here

1 个答案:

答案 0 :(得分:1)

解决方案:此问题是由于绑定造成的。所以我们需要在ajax调用之后重新绑定javascript

 $(document).on('click','.btn',function() {
        $("#step_two_billing_address").validate({
            rules: {
                "user_data[company]": "required",
                "user_data[company_gst]" :{
                    regex : '[0-9]{2}[a-zA-Z]{5}[0-9]{4}[a-zA-Z]{1}[0-9]{1}[zZ]{1}[a-zA-Z0-9]{1}',
                    maxlength : 15,
                },
                "user_data[b_zipcode]": "required",
                "user_data[b_phone]":{
                    required :true,
                    regex : '[6789][0-9]{9}',
                    maxlength : 10,
                } ,
                "user_data[b_address]": "required",
                "user_data[b_city]": "required",
                "user_data[b_state]": "required",
            },

            messages: {

                "user_data[company]": "Please enter your company name",
                "user_data[b_zipcode]": "Please enter your pincode",
                "user_data[phone]": {
                    required : "Please enter contact number",
                    regex    : "Please enter a valid contact number",
                    maxlength : "Please enter a valid contact number",
                },
                "user_data[b_address]": "Please enter billing address",
                "user_data[b_city]": "Please enter your city",
                "user_data[b_state]": "Please enter your state"
            },
            errorElement : 'span',
            errorPlacement: function(error, element) {
              element.addClass('error invalid');
              var label = $('#element').siblings('label');
              label.attr('class', 'active');
              var placement = $(element).data('error');
              if (placement) {
                $(placement).append(error)
              } else {
                error.insertAfter(element);
              }
            }
        });
    });