简体   繁体   English

密码文字,无需输入标签即可切换

[英]Password text & toggle without using Input Tag

How do you accomplish displaying text in dots using HTML, like you would see in a password field such as this: 您如何使用HTML完成以点显示文本,就像在密码字段中看到的那样:

Password: <input type="password" name="pwd">This text looks like dots</input>

Without using an <input> ? 使用<input>吗? I would like to utilize the redaction from a password field outside of the context of an input form. 我想利用输入表单上下文之外的密码字段中的修订。

Think of it like a page displaying a salary and I don't want to display the information in a text box, ie you don't want to give the impression that you can edit your salary, but when the page loads I don't want it to be visible right away in case someone is looking. 可以将其视为显示薪水的页面,并且我不想在文本框中显示信息,即,您不想给人留下您可以编辑薪水的印象,但是在加载页面时,我不会希望可以立即看到它,以防有人在看。 The text should show up in dots as such "Salary: ●●●●●●●● (check to view)" and then end up looking like "Salary: 54372.23 (check to hide)". 文本应以点显示,例如“薪水:●●●●●●●●(选中以查看)”,然后最终看起来像“薪水:54372.23(选中以隐藏)”。

A basic example could be: 一个基本的例子可能是:

$('[data-hidden-value] > .toggle').on('click', function () {
  var
    $wrapper = $(this).parent(),
    $display = $wrapper.find('.display'),
    revealed = $wrapper.data('revealed'),
    hiddenValue = String($wrapper.data('hidden-value'))
  ;

  $display.html(revealed ? hiddenValue.replace(/./g, '*') : hiddenValue);
  $wrapper.data('revealed', !revealed);
});

with the following html: 使用以下html:

Salary:

<span data-hidden-value="54372.23">
  <span class="display">********</span>
  <a class="toggle">(click to toggle)</a>
</span>

Demo: http://jsfiddle.net/MH2h6/ 演示: http//jsfiddle.net/MH2h6/

Based on your fiddle, it's pretty simple to get the toggling behavior by adding a quick check to see if you're displaying the hidden data or the redacted string: 根据您的小提琴,通过添加快速检查以查看是否显示隐藏数据或已编辑字符串来获得切换行为非常简单:

var $magics = $('.magic');
$magics.click( function () {
    var $magic = $(this);
    var password = $magic.data('password');
    $magic.text(($magic.text() == password) ? '****' : password);
});

See a demo . 观看演示

But this is fruitless, because you have embedded the sensitive information into your HTML. 但这是徒劳的,因为您已将敏感信息嵌入到HTML中。 If you want to do this right you're going to need to issue an AJAX request to your server to get the data you want. 如果你想这样做的权利 ,你会需要发出一个AJAX请求到服务器,以获得您想要的数据。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM