ajax 数据上的 jquery getElementById

发布于 2024-09-19 00:17:16 字数 763 浏览 8 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枕头说它不想醒 2024-09-26 00:17:16

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.

凹づ凸ル 2024-09-26 00:17:16

试试这个:

   function(data){
       alert( "Data Loaded: " + $('#username', data).html() );
   });

或者这个,取决于返回的 HTML 的结构:

   function(data){
       alert( "Data Loaded: " + $(data).filter('#username').html() );
   });

Try this:

   function(data){
       alert( "Data Loaded: " + $('#username', data).html() );
   });

Or this, depending on the structure of the HTML returned:

   function(data){
       alert( "Data Loaded: " + $(data).filter('#username').html() );
   });
半山落雨半山空 2024-09-26 00:17:16

你必须这样做:

function checkuser(user, wd, id, sub) {
  if(user.length < 7) {
    $("#"+id).html("Your username is too short. It must be longer than 6 characters");
    $("#"+sub).attr("disabled", true);
    return false;
  }
  else {
    $.post(
      wd + "register/checkuser/" + user,
      function(data){
        alert("Data Loaded: " + $(data).find("#username").html());
      }
    );
  }
}

You have to do this:

function checkuser(user, wd, id, sub) {
  if(user.length < 7) {
    $("#"+id).html("Your username is too short. It must be longer than 6 characters");
    $("#"+sub).attr("disabled", true);
    return false;
  }
  else {
    $.post(
      wd + "register/checkuser/" + user,
      function(data){
        alert("Data Loaded: " + $(data).find("#username").html());
      }
    );
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文