Jquery 当表单由 ajax 加载时验证功能不工作

Jquery validation function not working when form loaded by ajax

我在 cs-cart 结账中使用 Jquery 验证方法。 当我使用 cm-ajax class(css 默认 class)通过 ajax 请求移动到此步骤时,验证功能不起作用,但是如果我刷新页面显示所有错误。

$("#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);
          }
        }
    });

刷新页面后验证:

当我们移动到账单地址部分时验证显示弹出窗口 ajax

解决方案:此问题是由于绑定引起的。所以我们需要在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);
              }
            }
        });
    });