想要在JavaScript变量中存储密码类型

时间:2016-04-15 15:48:41

标签: javascript

<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();
}

这里当我使用输入类型=&#34;密码&#34;使用输入类型=&#34; text&#34;给了我值。所以我想如何使用输入类型=&#34;密码&#34;来获取值。

2 个答案:

答案 0 :(得分:0)

重写您的代码以不存储密码输入的值,而是存储密码输入元素的引用,以便您可以在xhr请求时使用

&#13;
&#13;
//  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();
}
&#13;
<input type="password" placeholder="Password name" id="box2">
&#13;
&#13;
&#13;

答案 1 :(得分:0)

默认情况下,当您刷新浏览器时,任何密码输入&#39;值设置为空字符串(出于安全原因)。

如果您希望保留该属性,则需要通过在输入本身上明确设置值来实现...

<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进行设置,如上所述。