简体   繁体   English

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

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

Edit: Sorry guys, I got the issue, Misspelled class name :( 编辑:抱歉,我遇到了这个问题,拼写错误的类名:(
Hey, I got a quick question for your guys here. 嘿,我在这里向您快速提问。 I have some anchor tags each of them points to a url of a certain user. 我有一些锚标记,每个标记都指向某个用户的网址。 The link id is the username. 链接ID是用户名。 All the links share a css class (.userset). 所有链接共享一个CSS类(.userset)。 When a user clicks the link I need to issue an Ajax request -using jQuery.ajax method- to a server resource passing in the id of the link. 当用户单击链接时,我需要使用jQuery.ajax方法向传递链接ID的服务器资源发出Ajax请求。 Here's how my code looks like: 这是我的代码的样子:
JS JS

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

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

This doesn't seem to work for me! 这似乎对我不起作用! this.id is always undefined. this.id始终是未定义的。
Yeah, I'm suer I'm missing something, so what am I missing dear SO Gus? 是的,我很想念我缺少的东西,那么我想念的是亲爱的古斯吗?

Edit (I was on the wrong track because of some probably incorrect assumptions) 编辑 (由于某些可能不正确的假设,我走在错误的轨道上)

Try the following: 请尝试以下操作:

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

你可以试试

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

You can try this: 您可以尝试以下方法:

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

Reading your request i can feel some confusion. 阅读您的请求,我会感到有些困惑。

Your request says: 您的要求说:

I have some anchor tags each of them points to a url of a certain user. 我有一些锚标记,每个标记都指向某个用户的网址。

With this sentence you mean you have many <a> in your code, with the href attribute pointing to some user-driven path. 用这句话意味着您的代码中有许多<a>href属性指向一些用户驱动的路径。

The link id is the username. 链接ID是用户名。 All the links share a css class (.userset). 所有链接共享一个CSS类(.userset)。

More info, why not. 更多信息,为什么不呢? Your tag now should look in my mind like <a class=".userset" id="myUserName" href="./some/url">content</a> 您的标记现在应该在我的脑海中看起来像<a class=".userset" id="myUserName" href="./some/url">content</a>

When a user clicks the link I need to issue an Ajax request -using jQuery.ajax method- to a server resource passing in the id of the link 当用户单击链接时,我需要使用传递jQuery ID的jQuery.ajax方法向服务器资源发出Ajax请求

Let's add the click event: 让我们添加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);
                }
              }
          );
      }
   );

With that being said, i can't really understand what your javascript code (with references to .img class ?) is referring to. 话虽这么说,我真的无法理解您的JavaScript代码(带有对.img类的引用)是指什么。

Hope my answer helped a bit though. 希望我的回答有所帮助。

The problem is that, I misspelled the class name so the returned wrapped set of my selector is undefined. 问题是,我拼错了类名,因此未定义选择器的返回包装集。 Thanks for your help all though. 一直感谢您的帮助。

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

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