javascript - 使用 JavaScript 的密码可见性
问题描述
我正在尝试检查该复选框是否被选中,如果是,那么它将显示密码。
这是我的代码
登录.html
<div class="form-check">
{{form.show_pass(class="form-check-input")}}
{{form.show_pass.label(class="form-check-label")}}
<script>
var x = document.getElementById('{{form.password.id}}');
var checkbox = document.getElementById('{{form.show_pass.id}}');
if (checkbox.checked=='true')
{
x.type = 'text';
}
</script>
</div>
表格.py
class LoginForm(FlaskForm):
email = StringField('Email',
validators=[DataRequired(), Email()])
password = PasswordField('Password',validators=[DataRequired()],id='pass')
remember = BooleanField('Remember Me')
show_pass = BooleanField('Show Password',id='show')
submit = SubmitField('Login')
解决方案
只需在页面加载后为显示密码元素添加一个事件侦听器:
window.addEventListener("load", function(){
var checkbox = document.getElementById('passwordCheckbox');
var x = document.getElementById('password');
checkbox.addEventListener('change', function() {
if(this.checked) {
x.type = 'text';
} else {
x.type = 'password';
}
});
});
<input type="checkbox" id="passwordCheckbox" /> Show Password
<br /><br />
<input type="password" id="password" />
推荐阅读
- unit-testing - 我应该在单元测试中模拟每个依赖项吗?
- c# - 通过 IIS 部署时 ASP.NET Core 应用程序未记录到文件
- loops - 如何使用 windows CMD 或 Batch 在所有子文件夹中递归运行多个命令?
- vb.net - Windows 10 上的 vb.net 程序要慢得多
- python - 查找名称包含固定列中特定值的列
- python - Python - 使用多线程解析和重复代理
- r - R中tbl_spark中的多种日期格式。我想将所有日期格式更改为一种格式(dd-mm-yyyy)
- react-native - 如何在本机反应中有效地显示大量图像,例如 instagram 提要屏幕?
- android - 如何获取android默认拨号盘呼叫断开按钮单击事件
- python - 在 Google OR-tools 中为模型添加额外的目标(装箱问题示例)