0

我的包含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

4

1 回答 1

1

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>

于 2019-08-26T12:59:58.277 回答