简体   繁体   English

使用 JavaScript 更改输入类型 HTML

[英]Change Input type HTML using JavaScript

<input type="password"> I want to run a JavaScript snippet that changes “password” to “text” is this possible? <input type="password">我想运行一个 JavaScript 片段,将“密码”更改为“文本”,这可能吗?

You can do it with pure Javascript or jQuery.您可以使用纯 Javascript 或 jQuery 来完成。

Let's assume your input is假设您的输入是

<input type="password" id="input1"/>

Pure Javascript:纯 Javascript:

document.getElementById('input1').setAttribute('type', 'text');

jQuery: jQuery:

$('#input1').attr('type', 'text');

First give the input element a unique 'id'.首先给输入元素一个唯一的'id'。

<input type='password' id='uniqueId'>

Then access the input element using javascript to change the input type.然后使用 javascript 访问输入元素以更改输入类型。

var input = document.getElementById('uniqueId')
input.setAttribute('type', 'text');

Another option as an extra:另一个选项作为额外的:

document.getElementById("id").type="text";

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

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