Wie kann ich ein Passwort-Feld zu Text und Passwort mit einem Kontrollkästchen deaktivieren deaktivieren?Passwortfeld in Text mit Kontrollkästchen mit jQuery ändern
Antwort
ist es das, was Sie suchen ??
<html>
<head>
<script>
function changeType()
{
document.myform.txt.type=(document.myform.option.value=(document.myform.option.value==1)?'-1':'1')=='1'?'text':'password';
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="txt" />
<input type="checkbox" name="option" value='1' onchange="changeType()" />
</form>
</body>
</html>
Verwenden Sie das onChange
Ereignis, wenn Sie das Kontrollkästchen ankreuzen und dann den Eingangstyp auf Text/Passwort umschalten.
Beispiel:
<input type="checkbox" onchange="tick(this)" />
<input type="input" type="text" id="input" />
<script>
function tick(el) {
$('#input').attr('type',el.checked ? 'text' : 'password');
}
</script>
Ich glaube, Sie
$('#inputField').attr('type','text');
anrufen und
$('#inputField').attr('type','password');
auf die Checkbox Zustand abhängig.
nicht möglich in IE, wenn man bedenkt IE nur-Set-it-einmal für das type-Attribut von Eingabeelementen herrschen . kein jQuery Bug –
Ich habe folgendes in der Produktion. Es klont ein neues Feld mit dem getoggten Typ.
toggle_clear_password = function(fields) {
// handles a list of fields, or just one of course
fields.each(function(){
var orig_field = $(this);
var new_field = $(document.createElement('input')).attr({
name: orig_field.attr('name'),
id: orig_field.attr('id'),
value: orig_field.val(),
type: (orig_field.attr('type') == 'text'? 'password' : 'text')
})
new_field.copyEvents(orig_field); // jquery 'copyEvents' plugin
orig_field.removeAttr('name'); // name clashes on a form cause funky submit-behaviour
orig_field.before(new_field);
orig_field.remove();
});
}
JQuery lässt Sie nicht nur das Typattribut nehmen und es ändern, zumindest nicht das letzte Mal, als ich es versuchte.
Toggle der Fokus Veranstaltung Checkbox und determain den Status der Checkbox und aktualisieren das Feld als nesscarry
$box = $('input[name=checkboxName]');
$box.focus(function(){
if ($(this).is(':checked')) {
$('input[name=PasswordInput]').attr('type', 'password');
} else {
$('input[name=PasswordInput]').attr('type', 'text');
}
})
aktualisiert: anschauliches Beispiel here
Typ mit $('#blahinput').attr('type','othertype')
Wechsel ist nicht möglich, in IE, wenn man bedenkt IE nur- Einmalige Regel für das type-Attribut von Eingabeelementen.
Sie müssen Texteingabe entfernen und Passworteingabe hinzufügen, umgekehrt.
$(function(){
$("#show").click(function(){
if($("#show:checked").length > 0){
var pswd = $("#txtpassword").val();
$("#txtpassword").attr("id","txtpassword2");
$("#txtpassword2").after($("<input id='txtpassword' type='text'>"));
$("#txtpassword2").remove();
$("#txtpassword").val(pswd);
}
else{ // vice versa
var pswd = $("#txtpassword").val();
$("#txtpassword").attr("id","txtpassword2");
$("#txtpassword2").after($("<input id='txtpassword' type='password'>"));
$("#txtpassword2").remove();
$("#txtpassword").val(pswd);
}
});
})
anschauliches Beispiel here
Update: Ich weiß nicht für die Zeit der Frage, aber Ihr erster Code funktioniert, wenn Sie versuchen, .prop ("Typ") heutzutage. – fredlegrain
@fredlegrain Nein, nicht auf IE8 –
<html>
<head>
<script>
$(function(){
$("#changePass").click(function(){
if ($("#txttext").hasClass("hide")){
$("#txttext").val($("#txtpass").val()).removeClass("hide");
$("#txtpass").addClass("hide");
} else if ($("#txtpass").hasClass("hide")){
$("#txtpass").val($("#txttext").val()).removeClass("hide");
$("#txttext").addClass("hide");
}
});
});
</script>
<style>
.hide{display:none;}
</style>
</head>
<body>
<form id="myform">
<input type="text" id="txtpass" type='password'/>
<input class="hide" type="text" id="txttext" type='text'/>
<button id="changePass">change</button>
</form>
</body>
</html>
oncheck
$('#password').get(0).type = 'text';
onuncheck
$('#password').get(0).type = 'password';
.attr ('Typ') wurde von jQuery Team blockiert, weil es nicht mit einigen Versionen von IE funktionieren wird.
Betrachten Sie diesen Code verwenden:
$('#inputField').prop('type','text');
$('#inputField').prop('type','password');
So viel einfacher umgesetzt werden können:
<form name="myform">
<input type="password" name="password" />
<input type="checkbox" name="showPassword" onchange="togglePasswordVisibility()" />
</form>
<script>
function togglePasswordVisibility() {
$('#password').attr('type', $('#showPassword').prop('checked') ? 'text' : 'password');
}
</script>
Works für jQuery 1.6+
können Sie etwas verwenden, wie dieses
$("#showHide").click(function() {
if ($(".password").attr("type")=="password") {
$(".password").attr("type", "text");
}
else{
$(".password").attr("type", "password");
}
});
Besuch hier für weitere http://voidtricks.com/password-show-hide-checkbox-click/
- 1. jQuery Tabellenfilter mit Text, Kontrollkästchen, wählt
- 2. Ändern von Text mit jQuery
- 3. Wie echo Kontrollkästchen Text mit Popup in Javascript/jQuery
- 4. Kontrollkästchen mit jQuery umschalten
- 5. Ändern Sie HTML-Text mit jquery (asp.net)
- 6. Ändern Sie die Option mit Kontrollkästchen aktiviert und jQuery
- 7. check/uncheck Kontrollkästchen mit jquery?
- 8. prüfen mehrere Kontrollkästchen mit Jquery
- 9. Ändern auswählen, um Kontrollkästchen mit Javascript
- 10. Text Shadow mit jquery
- 11. Ändern Sie den Text in pseudozufällige Farbe mit jquery
- 12. Mit Javascript/Jquery Text in div auf Schaltfläche ändern klicken
- 13. Bezeichnung für Passwortfeld
- 14. Benutzerdefiniertes Kontrollkästchen ist nicht mit Text ausgerichtet
- 15. DataGridCheckBoxColumn mit Text vor dem Kontrollkästchen
- 16. Textbereich mit jQuery ändern
- 17. Hintergrundfarbe ändern mit jquery
- 18. So entfernen Sie Eingabe-Kontrollkästchen mit jQuery
- 19. ändern Sie die Anzahl der Kontrollkästchen in HTML mit Javascript
- 20. Ändern des Kontrollkästchen-Symbols mit simple_form
- 21. ändern Sie Kontrollkästchen Rahmenfarbe
- 22. Toggle Text mit jQuery
- 23. Vertical Text mit jQuery
- 24. Wie Datenbank aktualisieren, während Kontrollkästchen mit jquery?
- 25. Schleife durch HTML-Kontrollkästchen mit jQuery
- 26. Validierung mit jQuery basierend auf einem Kontrollkästchen
- 27. Ändern von Divs mit Jquery
- 28. Änderung/Ändern Sie den Text von Dropdownlist-Wert mit Jquery
- 29. Wie mehrere Kontrollkästchen mit jQuery Ajax-Post
- 30. Wählen Sie alle Kontrollkästchen mit jQuery
Ihre Begrüßung :-) –