我目前正在将Placeholder Polyfill by Mathias Bynens与jquery.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>
答案 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
设置为“文本”而不是“密码”,有效地清除了那个讨厌的占位符问题。
次要问题:当开始在输入中输入文本时,密码的第一个字符会短暂显示为纯文本。