简体   繁体   English

jQuery.validate和隐藏的输入

[英]jQuery.validate and hidden input

I have a form with a pseudo-select element. 我有一个带有伪选择元素的表单。 The pseudo-select element is a dropdown menu. 伪选择元素是下拉菜单。 When one of the dropdown items is clicked, a hidden input is given a value assigned to that item. 单击其中一个下拉项时,会为隐藏输入指定一个分配给该项的值。 I'm using jQuery.validate to validate the form. 我正在使用jQuery.validate来验证表单。 A simple validation rule is bound to the hidden input. 一个简单的验证规则绑定到隐藏的输入。 If the input has no value, the hidden input and the pseudo-select are given an error class and an error message shows. 如果输入没有值,则隐藏输入和伪选择将被赋予错误类,并显示错误消息。

My problem is that changing the value of a hidden input does not trigger change, keyup, or blur events. 我的问题是,更改隐藏输入的值不会触发更改,键入或模糊事件。 If the error state is applied to the hidden input, that state remains after the input is given a valid value. 如果错误状态应用于隐藏输入,则在输入被赋予有效值之后该状态仍然存在。 Only when the form is again submitted is the input correctly validated. 只有在再次提交表单时才会正确验证输入。

Since jQuery.validate enables validation on hidden inputs, I'm wondering if there's a configuration or method that handles this issue. 由于jQuery.validate允许对隐藏输入进行验证,我想知道是否存在处理此问题的配置或方法。

EDIT: 编辑:

$form.validate({
    ignore: false,
    rules: {
        'firstname': 'required',
        'lastname': 'required',
        'org': 'required',
        'role': 'required',
        'email': {
            required: true,
            email: true
        }
    },
    messages: {
        'firstname': 'Please enter your first name',
        'lastname': 'Please enter your last name',
        'org': 'Please enter your organization',
        'role': 'Please select your role',
        'email': 'Please enter your email'
    },
    errorPlacement: function(error, element) {
        var $errMessage = $('.js-err-message');

        $errMessage.removeClass('js-hidden'); 

        $(error).each(function(){
            $errMessage.append(error);

            if (element.is('#role-input')) {
                $('.pseudo-select').addClass('mad-error');
            }
        });

    }
});

Using the jQuery Validation plugin, these are the standard events that will trigger validation on a particular element ( the last one is a bit different )... 使用jQuery Validation插件,这些是触发特定元素验证的标准事件( 最后一个有点不同 )......

  • typing in a box 在盒子里打字
  • selecting from a list 从列表中选择
  • clicking a checkbox or radio button 单击复选框或单选按钮
  • losing focus of the input element 失去输入元素的焦点
  • clicking the submit button ( entire form is validated ) 单击提交按钮( 整个表单已验证

Since none of those things are triggered when a hidden element changes value, you'll need to trigger it yourself by using the .valid() method to validate the element. 由于隐藏元素更改值时不会触发任何内容,因此您需要使用.valid()方法.valid()触发它以验证元素。

You've shown us no code whatsoever, let alone the code that changes the value of the hidden element, so I can only give you something generic. 你没有向我们展示任何代码,更不用说改变隐藏元素值的代码,所以我只能给你一些通用的东西。 You could place the .valid() code within whatever function you use to change the value... 您可以将.valid()代码放在用于更改值的任何函数中...

  • change the value of the hidden element 更改隐藏元素的值
  • call .valid() to validate the hidden element 调用.valid()来验证隐藏的元素

     $(document).ready(function() { $('#myform').validate({ // initialize plugin // your options }); $('#some_button').on('click', function() { // some imaginary button that changes the hidden value $('input[name="myHiddenElement"]').val('newvalue'); // <- change the hidden value $('input[name="myHiddenElement"]').valid(); // <- triggers validation on the hidden element }); }); 

You will likely need to use other plugin options like the errorPlacement option to properly place the error message label within your layout, otherwise it will float someplace after the hidden element. 您可能需要使用其他插件选项(如errorPlacement选项)将错误消息label正确放置在布局中,否则它将隐藏在隐藏元素之后的某个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM