英文:
how to write a code of show and hide password input field
问题
以下是要翻译的代码部分:
function myFunction() {
var data = document.getElementById("myInput");
if (data.type === "password") {
data.type = "text";
} else {
data.type = "password";
}
}
<div class="form-group col-md-6">
<label class="control-label">密码</label>
<input type="password" name="password password1" id="myInput" class="form-control eye" value="{{old('password')}}">
<i class="fa fa-eye" aria-hidden="true" onclick="myFunction()">显示/隐藏</i>
{!! $errors->first('password', '<p class="text-warning errorBag">:message</p>') !!}
</div>
英文:
In this question there are the following answers of this question but i give the best answer of this question.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function myFunction() {
var data = document.getElementById("myInput");
if (data.type === "password") {
data.type = "text";
} else {
data.type = "password";
}
}
<!-- language: lang-html -->
<div class="form-group col-md-6">
<label class="control-label">Password</label>
<input type="Password" name="password password1" id="myInput" class="form-control eye" value="{{old('password')}}">
<i class="fa fa-eye" aria-hidden="true" onclick="myFunction()"> Show Hide</i> {!! $errors->first('password', '<p class="text-warning errorBag">:message</p>') !!}
</div>
<!-- end snippet -->
Share my answer please
答案1
得分: 1
这是一个更简洁的方法:
var input = document.getElementById("myInput");
function myFunction()
{ input.type = input.type === "password" ? "text" : "password" }
英文:
Here is a shorter approach:
var input = document.getElementById("myInput");
function myFunction()
{ input.type = input.type === "password" ? "text" : "password" }
答案2
得分: 0
function myFunction() {
var data = document.getElementById("myInput");
var curType = data[0].getAttribute("type");
if (curType === "password") {
data[0].setAttribute("type", "text");
} else {
data[0].setAttribute("type", "password");
}
}
英文:
function myFunction() {
var data = document.getElementById("myInput");
var curType = data[0].getAttribute("type");
if (curType === "password") {
data.[0].setAttribute("type","text");
} else {
data.[0].setAttribute("type","password");
}
}
答案3
得分: 0
我在你的代码中没有发现任何错误或问题,你可以使用以下链接查看答案:https://www.w3schools.com/howto/howto_js_toggle_password.asp
英文:
I am not finding any mistake or wrong in your code, You can use following link for your answer: https://www.w3schools.com/howto/howto_js_toggle_password.asp
答案4
得分: 0
试一试:
<script type="text/javascript">
$(document).ready(function () {
$('#show_password').hover(function show() {
// 将属性更改为文本
$('#txtPassword').attr('type', 'text');
$('.icon').removeClass('fa fa-eye-slash').addClass('fa fa-eye');
},
function () {
// 将属性更改回密码
$('#txtPassword').attr('type', 'password');
$('.icon').removeClass('fa fa-eye').addClass('fa fa-eye-slash');
});
// 复选框显示密码
$('#ShowPassword').click(function () {
$('#Password').attr('type', $(this).is(':checked') ? 'text' : 'password');
});
});
</script>
英文:
Try This:
<script type="text/javascript">
$(document).ready(function () {
$('#show_password').hover(function show() {
//Change the attribute to text
$('#txtPassword').attr('type', 'text');
$('.icon').removeClass('fa fa-eye-slash').addClass('fa fa-eye');
},
function () {
//Change the attribute back to password
$('#txtPassword').attr('type', 'password');
$('.icon').removeClass('fa fa-eye').addClass('fa fa-eye-slash');
});
//CheckBox Show Password
$('#ShowPassword').click(function () {
$('#Password').attr('type', $(this).is(':checked') ? 'text' : 'password');
});
});
</script>
答案5
得分: 0
<div class="form-group col-md-6">
<label class="control-label">密码</label>
<input type="password" name="password password1" id="myInput" class="form-control eye" value="{{old('password')}}">
<button class="fa fa-eye" aria-hidden="true" onclick="myFunction()"> 显示/隐藏</button>
</div>
function myFunction() {
var data = document.getElementById("myInput");
if (data.type === "password") {
data.type = "text";
} else {
data.type = "password";
}
}
英文:
you can try this
html code
<div class="form-group col-md-6">
<label class="control-label">Password</label>
<input type="Password" name="password password1" id="myInput" class="form-control eye" value="{{old('password')}}">
<button class="fa fa-eye" aria-hidden="true" onclick="myFunction()"> Show Hide</button> </div>
css code
function myFunction() {
var data = document.getElementById("myInput");
if (data.type === "password") {
data.type = "text";
} else {
data.type = "password";
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论