使用 jQuery 更改输入字段的类型
$(document).ready(function() {
// #login-box password field
$('#password').attr('type', 'text');
$('#password').val('Password');
});
这应该更改 #password
输入字段(带有 id="password"
),其类型为 type
password
到普通文本字段,然后填写文本“密码”。
但这不起作用。为什么?
表格如下:
<form enctype="application/x-www-form-urlencoded" method="post" action="/auth/sign-in">
<ol>
<li>
<div class="element">
<input type="text" name="username" id="username" value="Prihlasovacie meno" class="input-text" />
</div>
</li>
<li>
<div class="element">
<input type="password" name="password" id="password" value="" class="input-text" />
</div>
</li>
<li class="button">
<div class="button">
<input type="submit" name="sign_in" id="sign_in" value="Prihlásiť" class="input-submit" />
</div>
</li>
</ol>
</form>
$(document).ready(function() {
// #login-box password field
$('#password').attr('type', 'text');
$('#password').val('Password');
});
This is supposed to change the #password
input field (with id="password"
) that is of type
password
to a normal text field, and then fill in the text “Password”.
It doesn’t work, though. Why?
Here is the form:
<form enctype="application/x-www-form-urlencoded" method="post" action="/auth/sign-in">
<ol>
<li>
<div class="element">
<input type="text" name="username" id="username" value="Prihlasovacie meno" class="input-text" />
</div>
</li>
<li>
<div class="element">
<input type="password" name="password" id="password" value="" class="input-text" />
</div>
</li>
<li class="button">
<div class="button">
<input type="submit" name="sign_in" id="sign_in" value="Prihlásiť" class="input-submit" />
</div>
</li>
</ol>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
甚至更容易......不需要创建所有动态元素。只需创建两个单独的字段,其中一个为“真实”密码字段(type =“password”),另一个为“假”密码字段(type =“text”),将假字段中的文本设置为浅灰色,然后将初始值设置为“密码”。然后使用 jQuery 添加几行 Javascript,如下所示:
因此,当用户输入“假”密码字段时,它将被隐藏,真实的字段将被显示,并且焦点将移动到真实的字段。他们永远无法在虚假字段中输入文本。
当用户离开真实密码字段时,脚本将查看它是否为空,如果是,将隐藏真实字段并显示假字段。
请注意不要在两个输入元素之间留有空格,因为 IE 会将一个输入元素放置在另一个输入元素之后一点(呈现空格),并且当用户进入/退出该字段时,该字段会出现移动。
Even easier... there's no need for all the dynamic element creation. Just create two separate fields, making one the 'real' password field (type="password") and one a 'fake' password field (type="text"), setting the text in the fake field to a light gray color and setting the initial value to 'Password'. Then add a few lines of Javascript with jQuery as below:
So when the user enters the 'fake' password field it will be hidden, the real field will be shown, and the focus will move to the real field. They will never be able to enter text in the fake field.
When the user leaves the real password field the script will see if it's empty, and if so will hide the real field and show the fake one.
Be careful not to leave a space between the two input elements because IE will position one a little bit after the other (rendering the space) and the field will appear to move when the user enters/exits it.
现在,您可以只使用
But 当然,您应该
在除 IE 之外的所有版本中执行此操作。也有一些占位符垫片可以模仿 IE 中的功能。
Nowadays, you can just use
But of course, you should really just do this
in all but IE. There are placeholder shims out there to mimic that functionality in IE as well.
一个更跨浏览器的解决方案......我希望这的要点可以帮助那里的人。
此解决方案尝试设置
type
属性,如果失败,它只会创建一个新的元素,保留元素属性和事件处理程序。
changeTypeAttr.js
(GitHub Gist):A more cross-browser solution… I hope the gist of this helps someone out there.
This solution tries to set the
type
attribute, and if it fails, it simply creates a new<input>
element, preserving element attributes and event handlers.changeTypeAttr.js
(GitHub Gist):这对我有用。
This works for me.
这对我有用。
This Worked for me.
使用 jQuery 的终极方法:
将原始输入字段从屏幕上隐藏起来。
通过 JavaScript 即时创建新的输入字段。
将 ID 和值从隐藏输入字段迁移到新输入字段。
要解决 IE 上的错误,如
type property无法更改
,您可能会发现这很有用,如下所示:将 click/focus/change 事件附加到新的输入元素,以便在隐藏时触发相同的事件输入。
旧的隐藏输入元素仍然位于 DOM 内部,因此会对新输入元素触发的事件做出反应。当 ID 被交换时,新的输入元素将像旧的输入元素一样运行,并响应对旧隐藏输入 ID 的任何函数调用,但看起来不同。
An ultimate way to use jQuery:
Leave the original input field hidden from the screen.
Create new input field on the fly by JavaScript.
Migrate the ID and value from hidden input field to the new input field.
To get around the error on IE like
type property cannot be changed
, you may find this useful as belows:Attach click/focus/change event to new input element, in order to trigger the same event on hidden input.
Old hidden input element is still inside the DOM so will react with the event triggered by new input element. As ID is swapped, new input element will act like the old one and respond to any function call to old hidden input's ID, but looks different.
您尝试过使用 .prop() 吗?
http://api.jquery.com/prop/
Have you tried using .prop()?
http://api.jquery.com/prop/
我还没有在 IE 中进行测试(因为我需要这个用于 iPad 网站) - 一个我无法更改 HTML 但我可以添加 JS 的表单:(
老式 JS 与所有 jQuery 相比很难看!)
但是, http://bugs.jquery.com/ticket/1957 链接到 MSDN:“从 Microsoft Internet Explorer 开始5、type 属性是一次读/写,但仅当使用 createElement 方法创建输入元素时且在将其添加到文档之前。”那么也许您可以复制该元素,更改类型,添加到 DOM 并删除旧的?
I haven't tested in IE (since I needed this for an iPad site) - a form I couldn't change the HTML but I could add JS:
(Old school JS is ugly next to all the jQuery!)
But, http://bugs.jquery.com/ticket/1957 links to MSDN: "As of Microsoft Internet Explorer 5, the type property is read/write-once, but only when an input element is created with the createElement method and before it is added to the document." so maybe you could duplicate the element, change the type, add to DOM and remove the old one?
只需创建一个新字段来绕过这个安全问题:
Just create a new field to bypass this security thing:
我在 Firefox 5 中尝试执行此操作时收到了相同的错误消息
。我使用下面的代码解决了该问题:
要使用它,只需将字段的 onFocus 和 onBlur 属性设置为如下所示:
我使用它作为用户名字段也是如此,因此它会切换默认值。调用函数时只需将第二个参数设置为“”即可。
另外可能值得注意的是,我的密码字段的默认类型实际上是密码,以防万一用户没有启用 JavaScript 或者出现问题,这样他们的密码仍然受到保护。
$(document).ready 函数是 jQuery,并在文档加载完成时加载。然后,这会将密码字段更改为文本字段。显然,您必须将“password_field_id”更改为密码字段的 ID。
欢迎使用和修改代码!
希望这对遇到我同样问题的每个人都有帮助:)
-- CJ Kent
编辑:
很好的解决方案,但不是绝对的。适用于 FF8 和 IE8,但不能完全适用于 Chrome(16.0.912.75 版本)。页面加载时,Chrome 不会显示密码文本。
另外 - 当自动填充打开时,FF 将显示您的密码。
I received the same error message while attempting to do this in Firefox 5.
I solved it using the code below:
And to use it, just set the onFocus and onBlur attributes of your fields to something like the following:
I use this for a username field as well, so it toggles a default value. Just set the second parameter of the function to '' when you call it.
Also it might be worth noting that the default type of my password field is actually password, just in case a user doesn't have javascript enabled or if something goes wrong, that way their password is still protected.
The $(document).ready function is jQuery, and loads when the document has finished loading. This then changes the password field to a text field. Obviously you'll have to change 'password_field_id' to your password field's id.
Feel free to use and modify the code!
Hope this helps everyone who had the same problem I did :)
-- CJ Kent
EDIT:
Good solution but not absolute. Works on on FF8 and IE8 BUT not fully on Chrome(16.0.912.75 ver). Chrome does not display the Password text when the page loads.
Also - FF will display your password when autofill is switched on.
对于所有想要在所有浏览器中使用该功能的人来说,这是一个简单的解决方案:
HTML
jQuery
Simple solution for all those who want the functionality in all browsers:
HTML
jQuery
类型属性无法更改,您需要用文本输入替换或覆盖输入,然后在提交时将值发送到密码输入。
Type properties can't be changed you need to replace or overlay the input with a text input and send the value to the password input on submit.
我想您可以使用包含“密码”一词的背景图像,并将其更改回
.focus()
上的空背景图像。.blur()
---->带有“密码”的图像.focus()
----->没有“密码”的图像您也可以使用一些 CSS 和 jQuery 来做到这一点。让一个文本字段正好显示在密码字段的顶部,hide() 位于 focus() 上并聚焦于密码字段...
I guess you could use a background-image that contains the word "password" and change it back to an empty background-image on
.focus()
..blur()
----> image with "password".focus()
-----> image with no "password"You could also do it with some CSS and jQuery. Have a text field show up exactly on top of the password field, hide() is on focus() and focus on the password field...
试试这个
演示在这里
Try this
Demo is here
这样工作起来就容易多了:
为了再次将其返回到密码字段,(假设密码字段是文本类型的第二个输入标签):
It works much easier with that:
and in order to turn it back to password field again,(assuming the password field is the 2nd input tag with text type):
使用这个非常简单
use this one it is very easy
这样就可以解决问题了。尽管可以改进以忽略现在不相关的属性。
插件:
用法:
小提琴:
http://jsfiddle.net/joshcomley/yX23U/
This will do the trick. Although it could be improved to ignore attributes that are now irrelevant.
Plugin:
Usage:
Fiddle:
http://jsfiddle.net/joshcomley/yX23U/
简单地说:
例如
这假设您的输入字段事先设置为“文本”。
Simply this:
such as
This is assuming that your input field was set to "text" before hand.
这是一个小片段,允许您更改文档中元素的
类型
。jquery.type.js
(GitHub Gist):它通过以下方式解决了这个问题从文档中删除
input
,更改type
,然后将其放回原来的位置。请注意,此代码段仅针对 WebKit 浏览器进行了测试 - 不保证其他任何内容!
Here is a little snippet that allows you to change the
type
of elements in documents.jquery.type.js
(GitHub Gist):It gets around the issue by removing the
input
from the document, changing thetype
and then putting it back where it was originally.Note that this snippet was only tested for WebKit browsers – no guarantees on anything else!
这是一种使用密码字段旁边的图像在看到密码(文本输入)和看不到密码(密码输入)之间切换的方法。我使用“睁眼”和“闭眼”图像,但您可以使用任何适合您的图像。它的工作方式是有两个输入/图像,单击图像后,该值将从可见输入复制到隐藏输入,然后交换它们的可见性。与许多使用硬编码名称的其他答案不同,这个答案足够通用,可以在页面上多次使用它。如果 JavaScript 不可用,它也会正常降级。
这是其中两个在页面上的样子。在此示例中,通过单击其眼睛即可显示密码 A。
Here is a method which uses an image next to the password field to toggle between seeing the password (text input) and not seeing it (password input). I use an "open eye" and "closed eye" image, but you can use whatever suits you. The way it works is having two inputs/images and upon clicking the image, the value is copied from the visible input to the hidden one, and then their visibility is swapped. Unlike many of the other answers which use hardcoded names, this one is general enough to use it multiple times on a page. It also degrades gracefully if JavaScript is unavailable.
Here is what two of these look like on a page. In this example, the Password-A has been revealed by clicking on its eye.
我只是做了以下操作来更改输入的类型:
并且它有效。
我需要这样做是因为我在 ASP NET Core 3.1 项目中使用 jQuery UI datepickers,并且它们在基于 Chromium 的浏览器上无法正常工作(请参阅:https://stackoverflow.com/a/61296225/7420301)。
I just did the following to change the type of an input:
and it works.
I was needing to do this because I was using jQuery UI datepickers' in an ASP NET Core 3.1 project and they were not working properly on Chromium-based browsers (see: https://stackoverflow.com/a/61296225/7420301).
我喜欢这种方式,改变输入元素的类型:old_input.clone()....
这是一个例子。有一个复选框“id_select_multiple”。如果将其更改为“selected”,则名称为“foo”的输入元素应更改为复选框。如果取消选中它,它们应该再次成为单选按钮。
I like this way, to change the type of an input element: old_input.clone()....
Here is an example. There is an check box "id_select_multiple". If this is is changed to "selected", input elements with name "foo" should be changed to check boxes. If it gets unchecked, they should be become radio buttons again.
这是一个 DOM 解决方案
heres a DOM solution
我创建了一个 jQuery 扩展来在文本和密码之间切换。适用于 IE8(可能也适用于 6 和 7,但未经测试),并且不会丢失您的值或属性:
像魅力一样工作。您可以简单地调用
$('#element').togglePassword();
在两者之间切换,或者提供一个选项以根据其他内容(例如复选框)“强制”操作:$('#element').togglePassword($checkbox.prop('checked'));
I've created a jQuery extension to toggle between text and password. Works in IE8 (probably 6&7 as well, but not tested) and won't lose your value or attributes:
Works like a charm. You can simply call
$('#element').togglePassword();
to switch between the two or give an option to 'force' the action based on something else (like a checkbox):$('#element').togglePassword($checkbox.prop('checked'));
这对我有用。
JS
This Worked for me.
JS
对于所有 IE8 爱好者来说,这是另一个选择,并且它在较新的浏览器中完美运行。您只需为文本着色以匹配输入的背景即可。如果您只有一个字段,当您单击/聚焦该字段时,颜色将更改为黑色。我不会在公共网站上使用它,因为它会“迷惑”大多数人,但我在管理员部分使用它,其中只有一个人可以访问用户密码。
-或者-
当您离开该字段时,这也需要将文本更改回白色。简单,简单,简单。
[ 另一种选择 ]
现在,如果您要检查多个字段,所有字段都具有相同的 ID(正如我所使用的那样),请将“pass”类添加到要隐藏文本的字段中。将密码字段类型设置为'文本'。这样,只有类为“pass”的字段才会被更改。
这是本文的第二部分。离开该字段后,这会将文本更改回白色。
Just another option for all the IE8 lovers, and it works perfect in newer browsers. You can just color the text to match the background of the input. If you have a single field, this will change the color to black when you click/focus on the field. I would not use this on a public site since it would 'confuse' most people, but I am using it in an ADMIN section where only one person has access to the users passwords.
-OR-
This, also needed, will change the text back to white when you leave the field. Simple, simple, simple.
[ Another Option ]
Now, if you have several fields that you are checking for, all with the same ID, as I am using it for, add a class of 'pass' to the fields you want to hide the text in. Set the password fields type to 'text'. This way, only the fields with a class of 'pass' will be changed.
Here is the second part of this. This changes the text back to white after you leave the field.
作为浏览器安全模型的一部分,此操作很可能被阻止。
编辑:确实,现在在 Safari 中测试,我收到错误
type property无法更改
。编辑2:这似乎是一个直接来自 jQuery 的错误。使用下面的直接 DOM 代码就可以了:
编辑 3:直接来自 jQuery 源代码,这似乎与 IE 有关(可能是一个错误,也可能是其安全模型的一部分,但 jQuery 并不具体):
It's very likely this action is prevented as part of the browser's security model.
Edit: indeed, testing right now in Safari, I get the error
type property cannot be changed
.Edit 2: that seems to be an error straight out of jQuery. Using the following straight DOM code works just fine:
Edit 3: Straight from the jQuery source, this seems to be related to IE (and could either be a bug or part of their security model, but jQuery isn't specific):
一步解决
One step solution