【发布时间】:2010-12-30 13:24:12
【问题描述】:
如何将密码字段切换为文本和密码,并取消选中复选框?
【问题讨论】:
标签: javascript jquery
如何将密码字段切换为文本和密码,并取消选中复选框?
【问题讨论】:
标签: javascript jquery
你可以使用这样的东西
$("#showHide").click(function () {
if ($(".password").attr("type")=="password") {
$(".password").attr("type", "text");
}
else{
$(".password").attr("type", "password");
}
});
访问这里了解更多http://voidtricks.com/password-show-hide-checkbox-click/
【讨论】:
这可以更简单地实现:
<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>
适用于 jQuery 1.6+
【讨论】:
检查
$('#password').get(0).type = 'text';
取消选中
$('#password').get(0).type = 'password';
【讨论】:
.attr('type') 被 jQuery 团队阻止,因为它不适用于某些版本的 IE。
考虑使用此代码:
$('#inputField').prop('type','text');
$('#inputField').prop('type','password');
【讨论】:
<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>
【讨论】:
这就是你要找的吗??
<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>
【讨论】:
更新:实时示例here
在 IE 中无法使用 $('#blahinput').attr('type','othertype') 更改类型,考虑到 IE 对输入元素的类型属性的唯一设置一次规则。
您需要删除文本输入并添加密码输入,反之亦然。
$(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 );
}
});
})
现场示例here
【讨论】:
我在生产中有以下内容。它克隆了一个具有切换类型的新字段。
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 不只是让您获取类型属性并更改它,至少我上次尝试时没有。
【讨论】:
切换复选框的焦点事件并确定复选框的状态并将字段更新为 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');
}
})
【讨论】:
相信你可以打电话
$('#inputField').attr('type','text');
和
$('#inputField').attr('type','password');
取决于复选框状态。
【讨论】:
勾选复选框时使用onChange事件,然后将输入的类型切换为文本/密码。
例子:
<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>
【讨论】: