2013-08-30 59 views
0

如何使用javascript onclick函數連續輸入密碼值到密碼字段?使用onclick函數將JavaScript值連續輸入到密碼輸入中?

我有兩個'藍'和'紅'的圖像與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 
    } 

請,我怎麼可以添加藍色圖像的值到密碼字段,無需更換紅色輸入...乾杯。 。評論歡迎..

+2

我感覺一些bruteforce web工具在做? –

+0

不是真的,但做類似的研究... –

+1

有沒有jQuery代碼,只是普通的Javascript,刪除該標籤。 – Barmar

回答

2

使用+=

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 
    } 
+0

謝謝你!...代碼完美工作..cheers –

1
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()

+0

謝謝...工作完美...讚賞! –

+1

那麼你如何標記我們的答案是正確的? – Soundz

+0

感謝您的提醒...... :) –