jQuery更改输入类型 [英] jQuery change input type
问题描述
尝试将 input
类型属性从 password
更改为 text
.
Trying to change input
type attribute from password
to text
.
$('.form').find('input:password').attr({type:"text"});
为什么这不起作用?
推荐答案
你不能用 jQuery 做到这一点,它明确禁止它 因为 IE 不支持它(检查你的控制台你会看到一个错误.
You can't do this with jQuery, it explicitly forbids it because IE doesn't support it (check your console you'll see an error.
如果这是您所追求的,您必须删除输入并创建一个新输入,例如:
You have to remove the input and create a new one if that's what you're after, for example:
$('.form').find('input:password').each(function() {
$("<input type='text' />").attr({ name: this.name, value: this.value }).insertBefore(this);
}).remove();
为了明确限制,jQuery 将不允许在 或
上更改
type
,因此行为是跨浏览器一致的(因为 IE 不允许这样做,他们决定在任何地方都不允许这样做).尝试时,您会在控制台中收到此错误:
To be clear on the restriction, jQuery will not allow changing type
on a <button>
or <input>
so the behavior is cross-browser consistent (since IE doens't allow it, they decided it's disallowed everywhere). When trying you'll get this error in the console:
错误:类型属性无法更改
这篇关于jQuery更改输入类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!