如何通过jQuery将普通字符更改为密码字符? [英] How to change normal character to password char by jquery?

查看:55
本文介绍了如何通过jQuery将普通字符更改为密码字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个文本框,我希望当用户在其中输入内容时,jQuery将在1秒钟后将每个字符转换为密码符号.

键入密码文本框时,在现代智能手机上会看到相同的效果.

有人有什么建议吗?我知道如何捕获按键并让用户输入字符.

<input type="text" id="username" name="username" />
<script type="text/javascript">
$("#username").keypress(function(event){   

// Get the keypress value
// ...?

var c = String.fromCharCode(e.which);

}

});
</script>

解决方案

您可以从以下内容开始: http://jsfiddle.net/89CF8/7/
但是,只有在您正确键入后才能起作用.
您应该将值保存在另一个变量中(在示例中,我使用letters数组),因为标准输入只允许您将其值更改为星号,从而丢失所有已键入的用户.

问题1.您应该添加非符号检查.例如,Del键会在letters数组中添加错误符号.

问题2.您应该添加退格键检查,并使用splice删除元素.

问题3.如果用户从文本中间删除符号,则很难将新值与存储初始值的变量同步.

毕竟,我认为这不是一个好主意,但是也许我的代码可以帮助您找到更好的解决方案.

Suppose I have a textbox and I want that when the user types inside it then each character will be converted to a password symbol after 1 sec by jQuery.

The same effect that we see on modern smart-phones when we type into password textboxes.

Does anyone have any suggestions? I know how to capture the key press and get the character user typed.

<input type="text" id="username" name="username" />
<script type="text/javascript">
$("#username").keypress(function(event){   

// Get the keypress value
// ...?

var c = String.fromCharCode(e.which);

}

});
</script>

解决方案

You can start with that: http://jsfiddle.net/89CF8/7/
But it works only if you type correctly.
You should save the value in another variable (I use letters array in the example), because standard input only allows you to change it's value for asterisks, losing all that user has been typed.

Issue #1. You should add checking for non-symbols. For example, Del key will add bad symbol to letters array.

Issue #2. You should add checking for backspace and use splice to remove elements.

Issue #3. If user deletes symbol from the middle of the text, it's very hard to synchronize new value with your variable, which stores the initial value.

After all, I think that it isn't good idea, but maybe my code will help you to find better solution.

这篇关于如何通过jQuery将普通字符更改为密码字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆