【问题标题】:MDL + jQuery validate integration: Work-around needed for mdl-radio__button validationMDL + jQuery 验证集成:mdl-radio__button 验证所需的解决方法
【发布时间】:2016-01-21 02:03:20
【问题描述】:

jQuery.validate 与 MDL 的正常用例似乎出现了问题。见this gist

这很好用:

<h1>Without MDL</h1>
<form id="foo">
  <input type="radio" name="bar" value="1" /> 1
  <input type="radio" name="bar" value="2" /> 2
<input type="submit" />
</form>
<script>
     $(function() {
         $('#foo').validate({
             rules: {
                 "bar": {
                     required: true
                 },
             },
             errorPlacement: function(error, element) {
                 console.log(element);
             }
         });
     });
</script>

什么都不做

<h1>With MDL</h1>
<form id="zha">
    <label for="baz__1" class="mdl-radio mdl-js-radio">
        <input type="radio" name="baz" value="1" id="baz__1" class="mdl-radio__button" /> 1
    </label>
    <label for="baz__2" class="mdl-radio mdl-js-radio">
        <input type="radio" name="baz" value="2" id="baz__2" class="mdl-radio__button" /> 2
    </label>
    <input type="submit" id="butt"/>
</form>
<script>
     $(function() {
         $('#zha').validate({
             rules: {
                 "baz": {
                     required: true
                 },
             },
             errorPlacement: function(error, element) {
                 console.log(element);
             }
         });
     });
</script>

附加jQuery.validator.setDefaults({ debug: true }) 没有效果——就像在 zero 调试输出中一样——在 MDL 版本中。删除 mdl-js-radiomdl-radio__button 使其按预期工作。我的直觉是 MDL 正在以一种断开 jQuery 对name= 属性的访问的方式更新 DOM,但我在 MDL 源代码中找不到任何证据支持这一点。

有人有一个可以在这里工作的集成吗?

【问题讨论】:

  • 引用:“我的直觉是 MDL 正在以一种断开连接的方式更新 DOM……” ~ 可能是这样的;那么为什么不简单地使用任何浏览器的 DOM 检查工具并找出答案呢?
  • 我做了,但是要么我的 DOM-fu 很弱,要么我的直觉错了。
  • 您没有将 mdl 版本的脚本包装在 jquery 函数中只是一个错字吗?
  • 是的,错字,谢谢!会更正。

标签: jquery jquery-validate material-design-lite


【解决方案1】:

经过一番研究,我想我找到了解决您问题的方法。这是一个有趣的。
对我来说,您的代码适用于 Firefox,但不适用于 Chrome 和 Safari。原因很简单。 MDL 删除输入单选按钮的 CSS 默认样式以隐藏它们(不显示:无,只有高度:0,宽度:0,不透明度......)。 Chrome 和 Safari 认为有“隐藏”。浏览 jQuery.validate 代码,我意识到输入单选按钮从未被发现。 (您的代码适用于 Chrome 和 safari 上的经典输入)。为什么?因为如果你看一下 jQuery.validate 的默认设置,你可以看到他忽略了隐藏的输入。

defaults: {
   ...
   ignore: ":hidden",
   ...
onfocusin: function( element, event ) {

还有过滤功能

elements: function() {
    var validator = this,
        rulesCache = {};

    // select all valid inputs inside the form (no submit or reset buttons)
    return $(this.currentForm)
    .find("input, select, textarea")
    .not(":submit, :reset, :image, [disabled]")
    .not( this.settings.ignore ) // This line
    .filter(function() {
        if ( !this.name && validator.settings.debug && window.console ) {
            console.error( "%o has no name assigned", this);
        }

        // select only the first element for each name, and only those with rules specified
        if ( this.name in rulesCache || !validator.objectLength($(this).rules()) ) {
            return false;
        }

        rulesCache[this.name] = true;
        return true;
    });
},

为了防止这种情况,只需添加这段代码:

jQuery.validator.setDefaults({
    ignore: ""
});

它对我有用。希望它对你有用。

【讨论】:

  • 不错的侦探!我很快会再次检查你的工作。
  • Tks。最后,这没什么大不了的。希望对您有所帮助。
猜你喜欢
  • 2017-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多