Jquery验证与占位符Polyfill不兼容? [IE9 Bug]

时间:2013-06-14 22:07:50

标签: jquery jquery-validate placeholder polyfills

我目前正在将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>

1 个答案:

答案 0 :(得分:0)

编辑:此修补程序在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设置为“文本”而不​​是“密码”,有效地清除了那个讨厌的占位符问题。

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