2012-01-09 2 views
0

パスワードフィールドをマスクしてアンマスクするためのチェックボックスを有効にした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); 

答えて

1

すべてこのプラグインがやっているが、パスワードフィールドの横に隠し入力フィールドを追加することです。チェックボックスをオンにすると、あるフィールドから別のフィールドに値がコピーされ、パスワードフィールドが隠され、追加されたフィールドが表示されます。

パスワードフィールドと新しく追加されたフィールドの両方にサイズを追加する必要があります。

追加された入力フィールドのクラスはspin_xxx(xxxは乱数)です。だから、このようなものが動作するはずです:

$('#passwordField').prev('input[class^="spin_"]').width(35); 

.showPasswordを呼び出しした後、この行を実行します。

0

$('#checkbox').click(function() { 
    if ($('#checkbox').is(':checked')) 
     $('#password-field').attr("type", "password"); 
    else 
     $('#password-field').attr("type", "text"); 
}); 
+0

入力フィールドのタイプ属性を実際に変更することはできません。 http://jsfiddle.net/SZFng/ –

+0

十分に公正ならば、同じ位置にテキストボックスが表示され、その中にはパスワードの値が表示されます。 –

+0

これはちょっとこのプラグインの機能です。別の入力を追加し、チェックボックスはその入力と元のパスワード入力を切り替えます。 –

関連する問題