简体   繁体   English

如何在输入字段中放置显示/隐藏图标

[英]How to put Show / Hide Icon inside input field

I have an input field type password, I'm trying to put show / hide icon inside it.我有一个输入字段类型密码,我正在尝试将显示/隐藏图标放入其中。 The problem is the icon, it is actually another input field and with html I can't put an input field inside another input field.问题是图标,它实际上是另一个输入字段,使用 html 我不能将输入字段放在另一个输入字段中。

Can someone help me ?有人能帮我吗 ? Maybe with css there can be a solution?也许使用 css 可以有一个解决方案? Sorry but I'm not very good with this, I appreciate any answer, thanks.对不起,但我不是很好,我很感激任何答案,谢谢。

 function showPassword() { var x = document.getElementById("password_current"); if (x.type === "password") { x.type = "text"; } else { x.type = "password"; } }
 /*Toggle Password class*/ #togglePw { display: none; } #togglePw + label:before { content: "\f06e"; } #togglePw:checked + label:before { content: "\f070"; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"> <p class=""> <label class="t2" for="password_current"></label> <input type="password" class="field-settings" name="password" id="password_current" autocomplete="off"/> <input type="checkbox" id="togglePw" onclick="showPassword()"/> <label for="togglePw" class="fa"></label> </p>

Just use some CSS to nudge it left a little, not forgetting to allow for padding on the input control so text won't cover it.只需使用一些 CSS 将其向左移动一点,不要忘记允许在输入控件上进行填充,以便文本不会覆盖它。

 function showPassword() { var x = document.getElementById("password_current"); if (x.type === "password") { x.type = "text"; } else { x.type = "password"; } }
 /*Toggle Password class*/ #togglePw { display: none; } #togglePw+label:before { content: "\f06e"; } #togglePw:checked+label:before { content: "\f070"; } /* add these: */ #togglePw+label { position: relative; left: -30px; cursor: pointer; } #password_current { padding-right: 30px; }
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"> <p class=""> <label class="t2" for="password_current"></label> <input type="password" class="field-settings" name="password" id="password_current" autocomplete="off" /> <input type="checkbox" id="togglePw" onclick="showPassword()" /> <label for="togglePw" class="fa"></label> </p>

I would suggest using absolute positioning to align the icon.我建议使用绝对定位来对齐图标。

Wrap the two inputs (password & checkbox) in a div.将两个输入(密码和复选框)包装在一个 div 中。

 <div id="password-input-toggle"> <input id="your-password-field"/> <input type="checkbox" id="your-toggle-checkbox"/> </div>

#password-input-toggle {
  position: relative;
}

#your-toggle-checkbox {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示/隐藏附加元素内的输入字段 - Show/Hide input field inside appended element 密码显示/隐藏 fontawesome 图标问题,未放入输入字段 - password show/hide fontawesome icon problem and not placed into input field 如果在输入字段中单击,angularjs将显示div,如果在外部单击,则将隐藏 - angularjs show div if clicked inside a input field and hide if clicked outside 如何在密码字段中使用图标以在 rails 中显示和隐藏密码 - How to icon in password field to show & hide password in rails 有没有办法可以将日历图标放在输入字段中? 还有,我怎样才能改变日历的颜色? - Is there a way in svelte where I can put the calendar icon inside input field? And, how can I change the color of the calendar? 输入字段内的可点击图标 - Clickable icon inside input field 如何使用 Bootstrap 和 jQuery 在输入字段内显示错误图标? - How to display error icon inside input field using Bootstrap and jQuery? 如何在已更新的输入字段内显示图标? - How to display an icon inside input field which got updated? 如何编写显示和隐藏密码输入字段的代码 - how to write a code of show and hide password input field React JS 如何在输入字段中显示密码 5 秒然后隐藏它? - React JS how to show password in input field for 5 seconds and then hide it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM