2016-06-23 87 views

回答

2

你應該試試這個:

$('#xd').on('mouseup', function() { 
    $('#Contraseña').attr('type', 'password') 
}); 

$('#xd').on('mousedown', function() { 
    $('#Contraseña').attr('type', 'text') 
}); 

Code here

1

使用此代碼

$("button").click(function(){ 
 
    if ($("input").attr("type") == "password"){ 
 
     $("input").attr("type", "text"); 
 
     $(this).text("Hide password"); 
 
    } else { 
 
     $("input").attr("type", "password"); 
 
     $(this).text("Show password"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="password" /> 
 
<button>Show password</button>

0

你可以只創建一個變量&它最初這個變量分配給false,現在就點擊更改true和切換attr type

var shown=false; 
    $('#xd').click(function() { 
     shown=!shown; 
     $('#Contraseña').attr('type', shown ? 'text' : 'password') 
    }); 
0

試試這個;)

<label for="pass_field_id">Password:</label> 
<input type="password" value="your_passowrd" name="password" id="pass_field_id" /> 
<a href="#" onclick="show_hide_password('pass_field_id');" id="showhide">Show</a> 

<script> 
function swapInput(tag, type) { 
    var el = document.createElement('input'); 
    el.id = tag.id; 
    el.type = type; 
    el.name = tag.name; 
    el.value = tag.value; 
    tag.parentNode.insertBefore(el, tag); 
    tag.parentNode.removeChild(tag); 
} 

function show_hide_password(target){ 
    var d = document; 
    var tag = d.getElementById(target); 
    var tag2 = d.getElementById("showhide"); 

    if (tag2.innerHTML == 'Show'){ 

     swapInput(tag, 'text'); 
     tag2.innerHTML = 'Hide'; 

    } else { 
     swapInput(tag, 'password'); 
     tag2.innerHTML = 'Show'; 
    } 
} 
</script>