简体   繁体   English

更改表中的输入类型

[英]changing type of input in a table

I'm trying to change the type of the displayed input Text in a row from text to password. 我正在尝试将连续显示的输入文本的类型从文本更改为密码。 It changes the type like I want, but the displayed text doesn't change from visible to invisible.. 它会像我想要的那样更改类型,但是显示的文本不会从可见更改为不可见。

 function changeType() { var x = document.getElementById("Table").rows[1].cells; var value = x[0].value; if (x[0].type == "password") { x[0].type = "text"; } else { x[0].type = "password"; } } 
 <table id="Table" class="table table-bordered table-dark table-striped table-hover"> <tr> <th>website</th> <th>username</th> <th> <input type="checkbox" onclick="changeType()"> password </th> </tr> <td><input type="password" value="1234567" readonly="readonly"></td> <td><input type="text" value="9876543" readonly="readonly"></td> <td><input type="text" value="simpleTest" readonly="readonly"></td> </table> 

You can make this very easy using querySelector on your own code as below: 您可以在自己的代码上使用querySelector使其非常简单,如下所示:

 function changeType() { var x = document.querySelector("input.password"); var value = x.value; if (x.type === "password") { x.type = "text"; } else { x.type = "password"; } } 
 <table id="Table" class="table table-bordered table-dark table-striped table-hover"> <tr> <th>website</th> <th>username</th> <th> <input type="checkbox" onclick="changeType()"> password </th> </tr> <td><input type="password" class="password" value="1234567" readonly="readonly"></td> <td><input type="text" value="9876543" readonly="readonly"></td> <td><input type="text" value="simpleTest" readonly="readonly"></td> </table> 

I hope I've been useful! 我希望我有用! :) :)

References: 参考文献:

Issues with your HTML and JavaScript are mentioned in comments: 注释中提到了HTML和JavaScript的问题:

  1. Missing <tr> around <td> elements. <td>元素周围缺少<tr>
  2. x refers to a table cell, not an input. x表示表格单元格,而不是输入。

In your context, I'd simplify by giving each element its own ID. 在您的上下文中,我将通过为每个元素提供自己的ID来简化。
That way you don't need to traverse the DOM. 这样,您就无需遍历DOM。

Feel free to use the inline onclick method in your original code. 随意在原始代码中使用内联onclick方法。
I've changed it to addEventListener out of my own preference. 我出于自己的喜好将其更改为addEventListener

 function toggleType(input) { input.type = (input.type == 'password') ? 'text' : 'password'; } var pass_input = document.getElementById("pass_input"); var pass_toggle = document.getElementById('pass_toggle'); pass_toggle.addEventListener('change', function() { toggleType(pass_input) }); 
 th { text-align: left; } label { cursor: pointer; } 
 <table> <thead> <tr> <th>website</th> <th>username</th> <th> <label><input type="checkbox" id="pass_toggle"> password</label> </th> </tr> </thead> <tbody> <tr> <td><input type="text" value="simpleTest" readonly="readonly"></td> <td><input type="text" value="9876543" readonly="readonly"></td> <td><input type="password" id="pass_input" value="1234567" readonly="readonly"></td> </tr> </tbody> </table> 

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

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