$(document).ready(function() {
    // #login-box password field
    $('#password').attr('type', 'text');
    $('#password').val('Password');
});

这是为了将password类型的#password输入字段(id="password")更改为普通的文本字段,然后填充文本"password"。

但这并不奏效。为什么?

表格如下:

<form enctype="application/x-www-form-urlencoded" method="post" action="/auth/sign-in">
  <ol>
    <li>
      <div class="element">
        <input type="text" name="username" id="username" value="Prihlasovacie meno" class="input-text" />
      </div>
    </li>
    <li>
      <div class="element">
        <input type="password" name="password" id="password" value="" class="input-text" />
      </div>
    </li>
    <li class="button">
      <div class="button">
        <input type="submit" name="sign_in" id="sign_in" value="Prihlásiť" class="input-submit" />
      </div>
    </li>
  </ol>
</form>

当前回答

简单的解决方案,所有人都想在所有浏览器的功能:

HTML

<input type="password" id="password">
<input type="text" id="passwordHide" style="display:none;">
<input type="checkbox" id="passwordSwitch" checked="checked">Hide password

jQuery

$("#passwordSwitch").change(function(){
    var p = $('#password');
    var h = $('#passwordHide');
    h.val(p.val());
    if($(this).attr('checked')=='checked'){
        h.hide();
        p.show();
    }else{
        p.hide();
        h.show();
    }
});

其他回答

这是所有IE8爱好者的又一个选择,在新浏览器上也能完美运行。您可以只给文本上色以匹配输入的背景。如果你有一个单一的字段,当你点击/聚焦在该字段上时,这将改变颜色为黑色。我不会在公共站点上使用这个,因为它会“迷惑”大多数人,但我在一个只有一个人可以访问用户密码的ADMIN部分中使用它。

$('#MyPass').click(function() {
    $(this).css('color', '#000000');
});

-OR-

$('#MyPass').focus(function() {
    $(this).css('color', '#000000');
});

这也是需要的,当您离开字段时,将文本更改为白色。简单,简单,简单。

$("#MyPass").blur(function() {
    $(this).css('color', '#ffffff');
});

[另一选择] 现在,如果你有几个字段,你正在检查,都有相同的ID,因为我正在使用它,添加一个类的“pass”字段,你想隐藏的文本。设置密码字段类型为“text”。这样,只有类为“pass”的字段才会被更改。

<input type="text" class="pass" id="inp_2" value="snoogle"/>

$('[id^=inp_]').click(function() {
    if ($(this).hasClass("pass")) {
        $(this).css('color', '#000000');
    }
    // rest of code
});

这是第二部分。这将在您离开字段后将文本更改为白色。

$("[id^=inp_]").blur(function() {
    if ($(this).hasClass("pass")) {
        $(this).css('color', '#ffffff');
    }
    // rest of code
});

这对我很有用。

$('#password').replaceWith($('#password').clone().attr('type', 'text'));

简单:

this.type = 'password';

$("#password").click(function(){
    this.type = 'password';
});

这是假设您的输入字段事先设置为“text”。

我喜欢这样改变输入元素的类型:old_input.clone().... 这里有一个例子。有一个复选框“id_select_multiple”。如果将其更改为"selected",则名称为"foo"的输入元素应更改为复选框。如果它得不到检查,他们应该再次成为单选按钮。

  $(function() {
    $("#id_select_multiple").change(function() {
     var new_type='';
     if ($(this).is(":checked")){ // .val() is always "on"
          new_type='checkbox';
     } else {
         new_type="radio";
     }
     $('input[name="foo"]').each(function(index){
         var new_input = $(this).clone();
         new_input.attr("type", new_type);
         new_input.insertBefore($(this));
         $(this).remove();
     });
    }
  )});

你试过使用.prop()吗?

$("#password").prop('type','text');

http://api.jquery.com/prop/