ajax 数据上的 jquery getElementById
我在 javascript 中有一个函数,它首先检查用户名是否是所需的长度,然后将用户名发送到页面进行测试,如果测试成功,将会出现 true
别的 false
我不断收到返回数据中不存在 getElementById 的错误
function checkuser(user, wd,id, sub)
{
if(user.length < 7)
{
document.getElementById(id).innerHTML = "Your username is too short. It must be longer than 6 charaters";
document.getElementById(sub).disabled = true;
return false;
} else {
$.post(wd + "register/checkuser/" + user,
function(data){
alert("Data Loaded: " + data.getElementById('username').innerHTML;
});
}
}
I got a function in javascript that first checks if the username is the required length and than sends the username to a page for a test if the test is a success there will be a<span id="username">true</span>
else<span id="username">false</span>
I keep getting an error that getElementById doesn't exist on the return data
function checkuser(user, wd,id, sub)
{
if(user.length < 7)
{
document.getElementById(id).innerHTML = "Your username is too short. It must be longer than 6 charaters";
document.getElementById(sub).disabled = true;
return false;
} else {
$.post(wd + "register/checkuser/" + user,
function(data){
alert("Data Loaded: " + data.getElementById('username').innerHTML;
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
data
返回值是一个字符串,而不是 DOMDocument - 因此它没有 getElementById 成员函数。更好的想法是让您的测试页返回 JSON,然后解析它。
如果您无法更改执行测试/返回的页面,那么您需要找到另一种方法来解析符合您目的的返回字符串。
The
data
return value is a string, not a DOMDocument - thus it doesn't have a getElementById member function.A better idea would be to have your test page return JSON instead, and parse that.
If you can't change the page that does the testing/returning, then you'll need to find another way to parse the return string that serves your purposes.
试试这个:
或者这个,取决于返回的 HTML 的结构:
Try this:
Or this, depending on the structure of the HTML returned:
你必须这样做:
You have to do this: