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