显示密码jQuery插件更改密码字段的长度
问题描述:
下载了showPassword jQuery plugin,它使复选框可以屏蔽和取消屏蔽密码字段。功能按预期工作,但当勾选复选框时,它将密码字段大小更改回默认值。取消选中复选框会使密码字段大小切换回HTML中指定的任何值(在我的例子中为35,默认值为20)。显示密码jQuery插件更改密码字段的长度
插件下载中的演示程序完成同样的事情,如果您向密码字段HTML添加大小,所以这绝对是插件的代码而不是我的HTML的问题。不幸的是,我对Javascript/jQuery的有限知识不足以诊断为什么此插件会根据复选框的状态更改密码字段长度。下面是插件的完整代码,非常感谢。
;(function($){
$.fn.showPassword = function(ph, options){
var spinput = $(this);
$.fn.showPassword.checker = function(cbid, inid){
$('input[id="'+cbid+'"]').click(function(){
if($(this).attr('checked')){
$('input.'+inid).val(spinput.val()).attr('id', spinput.attr('id')).attr('name',spinput.attr('name'));
$('input.'+inid).css('display', 'inline');
spinput.css('display', 'none').removeAttr('id').removeAttr('name');
}else{
spinput.val($('input.'+inid).val()).attr('id', $('input.'+inid).attr('id')).attr('name', $('input.'+inid).attr('name'));
spinput.css('display', 'inline');
$('input.'+inid).css('display', 'none').removeAttr('id').removeAttr('name');
}
});
}
return this.each(function(){
var def = { classname: 'class', name: 'password-input', text: 'Show Password' };
var spcbid = 'spcb_' + parseInt(Math.random() * 1000);
var spinid = spcbid.replace('spcb_', 'spin_');
if (spinput.attr('class') !== '') { var spclass = spinid+' '+spinput.attr('class'); }else{ var spclass = spinid; }
if(typeof ph == 'object'){ $.extend(def, ph); }
if(typeof options == 'object'){ $.extend(def, options); }
var spname = def.name;
// define the class name of the object
if(def.classname==''){ theclass=''; }else{ theclass=' class="'+def.clasname+'"'; }
// build the checkbox
$(this).before('<input type="text" value="" class="'+spclass+'" style="display: none;" />');
var thecheckbox = '<label><input'+theclass+' type="checkbox" id="'+spcbid+'" name="'+spname+'" value="sp" />'+def.text+'</label>';
// check if there is a request to place the checkbox in a specific placeholder.
// if not, place directly after the input.
if(ph == 'object' || typeof ph == 'undefined'){ $(this).after(thecheckbox); }else{ $(ph).html(thecheckbox); }
$.fn.showPassword.checker(spcbid, spinid);
return this;
});
}
})(jQuery);
答
所有这个插件做的是将旁边的密码字段一个隐藏的输入字段。选中该框后,该值将从一个字段复制到另一个字段,并且密码字段被隐藏,并显示添加的字段。
您需要为密码字段和新添加的字段添加大小。
添加的输入字段有一个类spin_xxx
(其中xxx是一个随机数)。所以,这样的事情应该工作:
$('#passwordField').prev('input[class^="spin_"]').width(35);
运行这行调用.showPassword
后。
答
你可以自己做这个功能有以下:
$('#checkbox').click(function() {
if ($('#checkbox').is(':checked'))
$('#password-field').attr("type", "password");
else
$('#password-field').attr("type", "text");
});
您实际上不能更改输入字段的类型属性。 http://jsfiddle.net/SZFng/ – 2012-01-09 18:58:12
公平的话,我会有一个文本框出现在同一位置的密码值在... – 2012-01-09 19:09:47
这是有点什么这个插件。它添加了另一个输入,复选框切换该输入和原始密码输入。 – 2012-01-09 19:20:05