php - 使用 onclick 函数连续将 JavaScript 值输入到密码输入中?

标签 php javascript html css

如何使用 javascript onclick 函数将密码值连续输入密码字段?

我有两个“蓝色”和“红色”图像可与 onclick 函数配合使用,并具有以下值;

Blue= w3!
Red= T4.

当我点击蓝色图像时,它输入值“w3!”进入密码字段。但是当我点击红色图像时,它会替换输入并在密码字段中变为“T4”。我如何在密码字段中附加红色输入和蓝色输入以变为“w3!T4”,而不是替换它?请问我在这里需要帮助...谢谢。 以下是我的代码条目:

 <!DOCTYPE html>
 <html>
 <head>
 <style>    
 # red
 {width:50px;
  height:50px;
  }
 # blue
 {width:50px;
  height:50px;
  }
 </style>

 <script src="javascript/myred.js"></script>
 <script src="javascript/myblue.js"></script>
 </head>

 <body>

 <img id="red" src="images/items/blue_in.jpg" alt="no blue" onclick="myblue()">
 <img id="blue" src="images/items/red_in.jpg" alt="no red" onclick="myred()">

 <label for="password">Passcode:</label>
 <input name="password" type="password" id="password">

 </body>
 </html> 

我的 JavaScript 文件存储在我的服务器中,如下所示;

对于 myblue:

  function myblue()
  {
  x=document.getElementById("password");  // Find the element
  x.value="w3!";    // add the content
  }

对于我的:

  function myred()
  {
  x=document.getElementById("password");  // Find the element
  x.value="T4";    // add the content
  }

请问,如何在不替换红色输入的情况下将蓝色图像的值添加到密码字段中...干杯...欢迎评论..

最佳答案

使用+=

function myblue()
  {
  x=document.getElementById("password");  // Find the element
  x.value += "w3!";    // add the content
  }

function myred()
  {
  x=document.getElementById("password");  // Find the element
  x.value += "T4";    // add the content
  }

关于php - 使用 onclick 函数连续将 JavaScript 值输入到密码输入中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18542038/

相关文章:

javascript - 在不到 2 秒内按下按钮的下一次单击时发出警报

php - 在php中将字符串转换为json

php - SF2 中的 INNER JOIN 与 Doctrine "createQueryBuilder"

javascript - 使用 JS 在本地编辑和保存文件

javascript - 伦敦时间与 GMT 不同的日期

javascript - 通过 Jquery 添加文本框在 Chrome 中不起作用

javascript - 在 iPhone 中加载 HTML 资源

php - Yii CJuiDatePicker - 页面上的第二个实例在第一个实例之后未直接打开

php - MySQLI 在生产中发生 fatal error ,但在开发中则不然

javascript - 如何在 ProcessingJS 中使用 mousePressed?