如何使用 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/