使用取决于("选择器").rules(' add',{....})

时间:2017-09-23 22:21:38

标签: jquery jquery-validate depends

在我的表单中,zipcode输入在State输入之前。因此,我想在填写州输入后才验证我的邮政编码。我已经让远程部分工作了。我无法弄清楚如何使用"依赖" jQuery Validation插件。由于我使用的是CRM,我需要使用.rules(' add',{...})设置验证的方式。这是我正在使用的代码:

      CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
        ".com_civicrm form[class^='CRM_'] #postal_code-1"
        ).rules('add', {
            remote: function () { 
                var r = {
                    url:    "http://localhost:81/example.org/media/plg_civcrm_frmvalidation/php/val-zip.php",
                    data:   {
                        address2: function() {
                            return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_street_address-5, " +
                                ".com_civicrm form[class^='CRM_'] #street_address-1"
                            ).val();
                        }, 
                        city: function() {
                            return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_city-5, " +
                                ".com_civicrm form[class^='CRM_'] #city-1"
                            ).val(); 
                        },
                        state: function() {
                            return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
                                ".com_civicrm form[class^='CRM_'] #state_province-1"
                            ).val();
                        },
                        zipcode: function() {
                            return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
                                ".com_civicrm form[class^='CRM_'] #postal_code-1"
                            ).val();
                        },
                    },
                    type: "post",

                    depends: function(element) {
                        return CRM.$(".option-com-civicrm form[class^='CRM_'] #billing_postal-code-5, " +
                                ".com_civicrm form[class^='CRM_'] #state_province-1"
                        ).val() !== '';
                    }
                };

                return r;
            }

        });

0 个答案:

没有答案