2016-04-15 49 views
-4
<input type="password" placeholder="Password name" id="box2"> 
var pass=document.getElementById('box2').value; 

xhttp = new XMLHttpRequest(); 
xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
    document.getElementById("formchek").innerHTML = xhttp.responseText; 
} }; 
xhttp.open("POST", "chek.php?msg="+pass, true); 
    xhttp.send(); 
} 

這裏,當我使用輸入類型=「密碼」我沒有得到密碼的值存儲在ID box2的文本框中使用上面的代碼,但使用輸入類型=「文本「給我的價值。所以我想如何使用輸入類型=」密碼「來獲取值。當想要在JavaScript變量中存儲密碼類型

+4

別喊。我們可以聽到你。 – Tushar

+2

'.value'在文本和密碼輸入方面的工作方式完全相同。 – Quentin

+0

也許這不起作用,因爲你的代碼是在你的輸入後立即在這個例子中?用戶不能填寫任何內容。嘗試'onchange'或'onsubmit' ... – somethinghere

回答

0

重寫代碼不要保存密碼輸入的值,而不是存儲密碼輸入元素的引用,這樣就可以在XHR請求

// instead of var pass=document.getElementById('box2').value; 
 
var pass=document.getElementById('box2'); 
 

 
xhttp = new XMLHttpRequest(); 
 
xhttp.onreadystatechange = function() { 
 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
 
    document.getElementById("formchek").innerHTML = xhttp.responseText; 
 
} }; 
 

 
// instead of just pass, use pass.value 
 
xhttp.open("POST", "chek.php?msg="+ pass.value, true); 
 
    xhttp.send(); 
 
}
<input type="password" placeholder="Password name" id="box2">

+0

heratidly thanx ......它工作正常......... thanx –

+0

在formchek id它顯示undefined –

0

默認情況下使用時間,你刷新你的瀏覽器,任何密碼輸入的值被設置爲空字符串(出於安全原因)。

如果你想保留這個屬性,那麼您可能需要顯式設置輸入本身的價值這樣做......

<input type="password" placeholder="Password name" id="box2" value="something"> 

的東西的價值通常會來自位服務器端腳本例如

<!-- using Classic ASP here, purely for example purposes --> 
<input type="password" placeholder="Password name" id="box2" value="<%=user.getPassword()>"> 

或者通過Javascript ...

document.getElementById('box2').value = 'something'; 

如果您是通過查詢字符串(不是一個好主意!)傳遞密碼回到頁面,那麼你就必須訪問從那裏輸入密碼並通過Javascript進行設置,如上所述。