javascript - 欧芹验证和标签

标签 javascript zurb-foundation silverstripe parsley.js

我的页面上有一个使用 Foundation-5 选项卡的表单。

当我加载页面时,出现以下错误:

您必须将 Parsley 绑定(bind)到现有元素上。

我认为这是因为调用欧芹时表单不在 Dom 中,因为非事件选项卡显示:无

$('form.parsley').parsley({ })

如何为我的表单启用 Parsley?

Parsley 目前的调用方式如下:

(function($) {
    $.entwine('ss.zenvalidator', function($) {   
        $('form.parsley').parsley({
             excluded: 'input[type=button], input[type=submit], input[type=reset], input[type=hidden], :hidden, .ignore-validation'
        });

        $('.field').entwine({

            getFormField: function() {
                var rtn = this.find('[name='+this.getFieldName()+'], [name="'+this.getFieldName()+'[]"]');
            },

            getFieldName: function() {
                return this.attr('id');
            },

            getFieldValue: function() {
                return this.getFormField().val();
            },

            evaluateEqualTo: function(val) {
                return this.getFieldValue() === val;
            },

            evaluateNotEqualTo: function(val) {
                return this.getFieldValue() !== val;
            },

            evaluateLessThan: function(val) {
                num = parseFloat(val);
                return this.getFieldValue() < num;
            },

            evaluateGreaterThan: function(val) {
                num = parseFloat(val);
                return parseFloat(this.getFieldValue()) > num;
            },

            evaluateContains: function(val) {
                return this.getFieldValue().match(val) !== null;
            },

            evaluateEmpty: function() {
                return $.trim(this.getFieldValue()).length === 0;
            },

            evaluateNotEmpty: function() {
                return !this.evaluateEmpty();
            },

            evaluateChecked: function() {
                return this.getFormField().is(":checked");
            }


        });


        $('.field.validation-logic').entwine({
            onmatch: function () {
                masters = this.getMasters();
                for(m in masters) {
                    this.closest('form').find('#'+masters[m]).addClass("validation-logic-master");
                }
            },

            getLogic: function() {
                return $.trim(this.getFormField().data('validation-logic-eval'));
            },

            parseLogic: function() {
                js = this.getLogic();
                result = eval(js);
                return result;
            },

            getMasters: function() {
                return this.getFormField().data('validation-logic-masters').split(",");
            }

        });


        $('.field.optionset').entwine({

            getFormField: function() {
                f = this._super().filter(":checked");
                return f;
            }

        });


        $('.field.optionset.checkboxset').entwine({

            evaluateHasCheckedOption: function(val) {
                this.find(':checkbox').filter(':checked').each(function() {
                    return $(this).val() === val || $(this).getLabel() === val;
                })
            },

            evaluateHasCheckedAtLeast: function(num) {
                return this.find(':checked').length >= num;
            },

            evaluateHasCheckedLessThan: function(num) {
                return this.find(':checked').length <= num;
            }
        });

        $('.field input[type=checkbox]').entwine({
            getLabel: function() {
                return this.closest('form').find('label[for='+this.attr('id')+']');
            }
        });

        $('.field.validation-logic.validation-logic-validate').entwine({
            testLogic: function() {
                this.getFormField().toggleClass('ignore-validation', this.parseLogic());
            }
        });


        $('.field.validation-logic.validation-logic-exclude').entwine({
            testLogic: function() {
                this.getFormField().toggleClass('ignore-validation', !this.parseLogic());
            }
        });

        $('.field.validation-logic-master :text, .field.validation-logic-master select').entwine({
            onmatch: function() {
                this.closest(".field").notify();
            },

            onchange: function() {
                this.closest(".field").notify();
            }
        });

        $('.field.validation-logic-master :checkbox, .field.validation-logic-master :radio').entwine({
            onmatch: function() {
                this.closest(".field").notify();
            },

            onclick: function() {
                this.closest(".field").notify();
            }
        });

        $('.field.validation-logic-master').entwine({
            Listeners: null,

            notify: function() {
                $.each(this.getListeners(), function() {
                    $(this).testLogic();
                });
            },

            getListeners: function() {
                if(l = this._super()) {
                    return l;
                }
                var self = this;
                var listeners = [];
                this.closest("form").find('.validation-logic').each(function() {
                    masters = $(this).getMasters();
                    for(m in masters) {
                        if(masters[m] == self.attr('id')) {
                            listeners.push($(this));
                            break;
                        }
                    }
                });
                this.setListeners(listeners);
                return this.getListeners();
            }
        });
    });

})(jQuery);

最佳答案

我认为以下内容应该足够了:(未经测试)

$('form.parsley').entwine({
    onmatch: function() {
        $(this).parsley({
            excluded: 'input[type=button], input[type=submit], input[type=reset], input[type=hidden], :hidden, .ignore-validation'
        });
    }
});

关于javascript - 欧芹验证和标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24425369/

相关文章:

javascript - 根据父 div 的 id 添加升序图像

javascript - 从超链接单击添加文本框和标签

html - Zurb Foundation 多列网格高度问题

css - 对特异性基础的误解 5

javascript - 在SAPUI5中,如何根据另一个表设置表列宽?

javascript - 基础标题 Accordion 的内部链接?

html - 表单 CSS 问题。 silverstripe 用户自定义表单

php - 升级到 Silverstripe 4.1.0 出现 fatal error : Class 'PageController' not found in ErrorPageController. php?

php - 如何以编程方式向 Silverstripe 添加新页面?

javascript - Node 不提供静态目录