如何使用複選框選中取消選中密碼字段爲文本和密碼?使用jQuery複選框將密碼字段更改爲文本
回答
這是你在尋找什麼?
<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>
勾選複選框時使用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>
我相信你可以打電話
$('#inputField').attr('type','text');
和
$('#inputField').attr('type','password');
取決於複選框狀態。
無法在IE中,考慮到IE唯一的,設置它曾經統治輸入元素的類型屬性。不是一個jQuery的bug – 2009-12-28 18:44:36
我在生產中有以下幾項。它克隆了一個具有切換類型的新字段。
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不只是讓你取得type屬性並改變它,至少不是我最後一次嘗試。
切換複選框的焦點事件和determain複選框的狀態並更新字段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');
}
})
更新:活生生的例子here
可更換式與$('#blahinput').attr('type','othertype')
是不可能在IE中,考慮到IE的only-爲輸入元素的type屬性設置一次規則。
您需要刪除文本輸入並添加密碼輸入,反之亦然。
$(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
更新:我不知道這個問題的時間,但如果你現在嘗試使用.prop('type'),你的第一個代碼就可以工作。 – fredlegrain 2012-11-05 10:23:20
@fredlegrain不要在IE8 – 2015-01-09 15:07:13
<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( '類型')被阻斷jQuery開發團隊,因爲它不會與IE瀏覽器的一些版本。
考慮使用此代碼:
$('#inputField').prop('type','text');
$('#inputField').prop('type','password');
這可以更簡單的實現:
<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+
你可以使用一些這樣的事
$("#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/
- 1. 使用jQuery將HTML文本輸入更改爲密碼輸入
- 2. 如何使用複選框更改文本框文本模式
- 3. 複選框更改文本裝飾輸入字段
- 4. 將字段更改爲GridView中的複選框
- 5. 使用複選框更新輸入文本字段
- 6. 焦點/類型的密碼輸入字段從文本更改爲密碼?
- 7. 使輸入文本字段可更改和可選(切換複選框)
- 8. 將選項文本更改爲值jquery
- 9. Jquery UI複選框作爲按鈕更改文本值
- 10. 顯示密碼jQuery插件更改密碼字段的長度
- 11. jQuery更改複選框
- 12. Jquery複選框更改
- 13. 將短文本字段顯示爲複選框
- 14. 如果複選框被選中,則更改文本框文本
- 15. jquery更改複選框更改css類
- 16. 如何使用jQuery將標籤更改爲文本框
- 17. 通過使用jQuery將標籤更改爲空文本框
- 18. 通過使用jQuery將標籤更改爲文本框?
- 19. 根據複選框值更改文本
- 20. 更改複選框的文本大小
- 21. 將文本字段更改爲選擇和默認值
- 22. 將複選框標籤複製到隱藏文本字段
- 23. 密碼文本字段
- 24. jquery禁用使用複選框的文本框(複選框爲多個文本框)
- 25. 將選中的複選框值插入文本字段
- 26. 如何從JavaScript更改密碼字段的文本?
- 27. 用jquery更改複選框的樣式
- 28. 如何將密碼字段更改爲星號而不是點
- 29. jquery - 將文本框更改爲點擊文本
- 30. 使用jQuery更改選項的文本
歡迎您:-) – 2009-12-28 19:50:45