繁体   English   中英

如何发送此锚的ID作为函数参数?

[英]How to send the Id of this anchor as a function parameter?

编辑:抱歉,我遇到了这个问题,拼写错误的类名:(
嘿,我在这里向您快速提问。 我有一些锚标记,每个标记都指向某个用户的网址。 链接ID是用户名。 所有链接共享一个CSS类(.userset)。 当用户单击链接时,我需要使用jQuery.ajax方法向传递链接ID的服务器资源发出Ajax请求。 这是我的代码的样子:
JS

 $(".columnist_set .img").click(function() {

        alert("this.id =" + this.id);
        var x = track(this.id);
});

这似乎对我不起作用! this.id始终是未定义的。
是的,我很想念我缺少的东西,那么我想念的是亲爱的古斯吗?

编辑 (由于某些可能不正确的假设,我走在错误的轨道上)

请尝试以下操作:

$(".columnist_set .img").click(function() {
        var id = $(this).attr("id");
        alert("id =" + id);
        var x = track(id);
});

你可以试试

$(this).attr("id");

您可以尝试以下方法:

$(".userset").click(function() {
   var x = track($(this).attr("id"));
});

阅读您的请求,我会感到有些困惑。

您的要求说:

我有一些锚标记,每个标记都指向某个用户的网址。

用这句话意味着您的代码中有许多<a>href属性指向一些用户驱动的路径。

链接ID是用户名。 所有链接共享一个CSS类(.userset)。

更多信息,为什么不呢? 您的标记现在应该在我的脑海中看起来像<a class=".userset" id="myUserName" href="./some/url">content</a>

当用户单击链接时,我需要使用传递jQuery ID的jQuery.ajax方法向服务器资源发出Ajax请求

让我们添加click事件:

   //all the anchor tags with "userset" class
   $('a.userset').click(
      function(){
          //doing the ajax call
          $.ajax({
              type: "POST",
              url: $(this).attr("href"),      //picking the href url from the <a> tag
              data: "id=" + $(this).attr("id"),    //sanitize if it's a free-inputed-string
              success:
                function(result) {
                    alert("I AM THE MAN! Data returned: " + result);
                }
              }
          );
      }
   );

话虽这么说,我真的无法理解您的JavaScript代码(带有对.img类的引用)是指什么。

希望我的回答有所帮助。

问题是,我拼错了类名,因此未定义选择器的返回包装集。 一直感谢您的帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM