gpt4 book ai didi

Jquery 不将 var 打印为字符串

转载 作者:行者123 更新时间:2023-12-01 06:48:49 26 4
gpt4 key购买 nike

我正在尝试进行基本的 ajax 登录,但我一生都无法弄清楚为什么当我执行 toString() 时,uname 和 pword 变量不断打印 [object HTMLInputElement]

You can see a working example here

只需输入一些内容并单击“登录”即可。它将访问数据库,然后打印出用户名、密码,然后是服务器响应

   <script>

function login() {

var xmlhttp;
var uname = "";
var pword = "";
var date;

if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}

else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

uname = document.getElementById("username").toString();
pword = document.getElementById("password").toString();
date = new Date().toLocaleDateString() + " " + new Date().toLocaleTimeString();
xmlhttp.open("GET", "login.php?uname=" + uname.toString() + "&pword=" + pword.toString() + "&date=" + date, true);
xmlhttp.send();

xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("response").innerHTML = uname.toString() + "<br/>" + pword.toString() + "<br/>" + date + "<br/>" + xmlhttp.responseText;
}
}

}
</script>

这是 HTML 代码

   <body>
<center>
<div style="align-content:center">
<form>
<br />
<br />
<br />
<br />
<h1 style="font-family:'Microsoft Yi Baiti'; font-size:40px">Ajax test Site</h1>
<br />
<label>Username</label>
<br />
<input type="text" id="username" name="username" style="width:190px"/>
<br />
<label>Password</label>
<br />
<input type="password" id="password" name="password"style="width:190px"/>
<br />
<label id="tryAgainText" style="color:red; visibility:hidden">Username or Password incorrect</label>
<br />
<input onclick="login()" type="button" name="submit" value="Login" style="width:216px; margin-left: 0px;"/>
</form>
<p id="response">This is the response field</p>
</div>
</center>
</body>

最佳答案

您正在选择 html 元素来代替其中存在的值。应该是这个

uname = document.getElementById("username").value.toString();
pword = document.getElementById("password").value.toString();

关于Jquery 不将 var 打印为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18299657/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com