【问题标题】:Jquery Validate Incompatible with Placeholder Polyfill? [IE9 Bug]Jquery Validate 与占位符 Polyfill 不兼容? [IE9错误]
【发布时间】:2013-06-14 22:07:49
【问题描述】:

我目前正在将Placeholder Polyfill by Mathias Bynensjquery.validate plugin (v1.11.1) 结合使用,并且在使用 Internet Explorer 9 时遇到了令人沮丧的错误。

如果我的type="password" 字段定义了非空的placeholder 属性,我无法获得必填字段 验证。一旦输入了一个值并且占位符文本消失了,剩下的验证就可以正常工作了。

Live Demo of this code can be found here

JS

$('input').placeholder(); // init placeholder plugin
signupFormValidator = $('form').validate(); // init validation

HTML

<form>
  <input class="required" type="text" name="test" id="test" placeholder="test"><br>
  <input class="required" type="password" name="password" id="password" placeholder="password"><br>
  <input class="required" type="password" name="confirm"  id="confirm"  placeholder="confirm"><br>
  <input type="submit">
</form>

【问题讨论】:

  • 你有没有找到解决这个问题的方法?
  • 不幸的是没有干净的解决方案,但我最终使用了一些 JS 的变通方法。在下面查看我的新答案。
  • @creativetim 仅供参考 - 刚刚发现我的 hack/fix 在 FF 中被破坏了。回到绘图板。

标签: jquery jquery-validate placeholder polyfills


【解决方案1】:

编辑:此修复在 Firefox 中引入了错误。请参阅下面的评论。


为了解决这个问题,我最终使用了一些我自己烘焙的相当 hacky 的 jQuery:

$('#password, #confirm').keyup(function() {
    var $input = $(this);
    if ($input.attr('type') === 'text') {
        if ($input.val().length > 0) {
            $input.attr('type', 'password');
        }
    } else if ($input.val() === "") {
        $input.attr('type', 'text');
    }
});

这样做的目的是将字段type 设置为“文本”而不​​是“密码”,从而有效地解决了那个讨厌的占位符问题。

小问题:当开始在输入中输入文本时,密码的第一个字符会短暂显示为纯文本。

【讨论】:

  • 仅供参考,刚刚发现这(可能)引入了一个错误(目前仅在 Firefox 中重现)。当快速输入密码字段时,光标有时会出现在单词的中间。我会回复任何进一步的发现/解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 1970-01-01
  • 2012-12-30
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多