[英]Change type for input id dynamically from external JavaScript file
我的page.php
包含div id="container"
与id="input"
其默认类型是input type="text"
:
<div class="container">
<div class="row">
<div class="col-75">
<div id="container">
<input type="text" id="input" value="" name="password">
</div>
<input type="submit" value="OK" id="logBtn">
</div>
</div>
</div>
我想将input type="text"
更改为input type="password"
对于相同的输入id="input"
并接收值textToPassType(1)
:
function textToPassType(val1){
if (val1 == 1){
document.getElementById('input').type = 'password';
} else if (val1 == 0){
document.getElementById('input').type = 'text';
}
}
这种方法仅在我将此函数包含到page.php
,但如果在外部文档中使用它: <script type="text/javascript" src="myjs.js"></script>
方法不起作用。 所以,我正在寻找一些正确的方法来动态更改输入类型并将相同的 id 名称从外部myjs.js
到我的page.php
var
是 JavaScript 中的一个关键字,用来声明一个变量,你不能用它作为变量名,试着命名你的变量val
什么的:
function textToPassType(val) { if (val == 1) { document.getElementById('input').type = 'password'; } else if (val == 0) { document.getElementById('input').type = 'text'; } } textToPassType(1);
<div class="container"> <div class="row"> <div class="col-75"> <div id="container"> <input type="text" id="input" value="" name="password"> </div> <input type="submit" value="OK" id="logBtn"> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.