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


Continuously input JavaScript values into password entry with onclick function?

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

我有两个"蓝色"answers"红色"图像,它们与onclick函数一起使用,并且具有以下值;

Blue= w3!
Red= T4.

当我点击蓝色图像时,它会输入值"w3!"输入密码字段。但当我点击红色图像时,它会替换输入,并在密码字段中变为"T4"。如何将红色输入与蓝色输入连接起来,使其成为'w3!T4’,而不是替换它?求你了,我这里需要帮助。。。thx。以下是我的代码条目:

 <!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
  }

对于myred:

  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
  }
function myblue()
{
  x=document.getElementById("password");  // Find the element
  x.value += "w3!";    // add the content
}

当然,该解决方案会附加字符串,表示它可能是"w3!w3!w3!T4!",具体取决于您单击它的频率。

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

如果只需要一次值,请使用此方法。当然,您必须更改此函数才能用作myred()