简体   繁体   English

错误放置,Select2

[英]Error placement, Select2

I have a problem with select2 error placement when I save a form containing one or many select2. 保存包含一个或多个select2的表单时, select2错误位置出现问题。

Here is what I have: I have some Forms with some mandatory select2 inputs and they work fine showing the error with jQuery Validate but whatI want is the html to jump to the select2 with the error when I save the form. 这是我所拥有的:我有一些带有一些强制性select2输入的表单,它们可以很好地显示jQuery Validate的错误,但是我想要的是当我保存表单时html跳转到带有错误的select2的方法。 Currently the submit handler jumps just fine to any other element except the selectors. 当前,提交处理程序跳到除选择器之外的任何其他元素上都很好。

I ended up overwriting the invalid handler and call the id of the select2: 我最终覆盖了无效的处理程序,并调用了select2的ID:

invalidHandler: function(form, validator) {
    var errors = validator.numberOfInvalids();
    if (errors) {
        if($(validator.errorList[0].element).is(":visible"))
        {
            $('html, body').animate({
                scrollTop: $(validator.errorList[0].element).offset().top
            }, 100);
        }
        else
        {
            $('html, body').animate({
                scrollTop: $("#s2id_" + $(validator.errorList[0].element).attr("id")).offset().top
            }, 100);
        }
    }
}

Use the same jQuery validator, simply move the error 使用相同的jQuery验证程序,只需移动错误

$("#your_id").change(function(e) {
   let id_error = `#${$(this).attr('id')}-error`
   if(!$(this).valid()) {
      $(id_error).detach().appendTo($('.select2-container').first())
   }
})

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

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