如何使用 javascript / jquery 增加点击次数?

发布于 2025-01-01 08:21:35 字数 1326 浏览 6 评论 0原文

我有 5 个在循环中创建的链接 ($.each):

$.each(data, function(index, element) {
name = element.name;
id = element.id;
    $('<a href="#" id="'+id+'" onClick="submitNotification('+id+');">'+name+'</a>')

});

在我的情况下,这将创建 5 个链接:

<a href="#" id="1" >name1</a>
<a href="#" id="2" >name2</a>
<a href="#" id="3" >name3</a>
<a href="#" id="4" >name4</a>
<a href="#" id="5" >name5</a>

我想要的是

function submitNotification(cdata){
alert('you selected '+cdata->name+' on '+place+'place');

         $.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/test.php', 
    data: cdata,
    dataType:'json',
    success: function (data) {
    ...
    }

});
}

当我单击任何链接时获取警报:you selected name2 on 1 place。然后,如果我单击另一个链接来获取相同的警报,但 place 变量会增加 1。

换句话说,每次我点击一个链接时,我都会得到从 1 开始的 place 并递增 1 直到达到 5

,然后将数据发送到 ajax post。

现在,有两件事我很难做:

1. placing id and name inside an javascript object so i they both can be available to send to the ajax post
2. how do i do the increment so that no matter on what link i click first the countwill start from 1. 

有什么想法吗?知道这一点会对我有很大帮助。

谢谢

i have 5 links that gets created in a loop ($.each):

$.each(data, function(index, element) {
name = element.name;
id = element.id;
    $('<a href="#" id="'+id+'" onClick="submitNotification('+id+');">'+name+'</a>')

});

in my case this will create 5 links:

<a href="#" id="1" >name1</a>
<a href="#" id="2" >name2</a>
<a href="#" id="3" >name3</a>
<a href="#" id="4" >name4</a>
<a href="#" id="5" >name5</a>

and

function submitNotification(cdata){
alert('you selected '+cdata->name+' on '+place+'place');

         $.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/test.php', 
    data: cdata,
    dataType:'json',
    success: function (data) {
    ...
    }

});
}

what i want is when i click on any link to get the alert: you selected name2 on 1 place. Then if i click another link to get the same alert but with the place var incremented by one.

In other words Each time i click on a link i get place starting at 1 and increment 1 until it gets to 5

then send data to a ajax post.

Now, there are 2 things i have dificulty doing:

1. placing id and name inside an javascript object so i they both can be available to send to the ajax post
2. how do i do the increment so that no matter on what link i click first the countwill start from 1. 

any ideas? knowing this will help me a lot.

THanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

放血 2025-01-08 08:21:35

在函数外部声明位置计数器和数据容器:

var place = 0, postData = [];

function submitNotification(cdata){
  place++;
  alert('you selected '+cdata->name+' on '+place+'place');

  postData.push(cdata);

  if (place == 5) {

         $.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/test.php', 
    data: { places: postData },
    dataType:'json',
    success: function (data) {
        ...
      }
    });

  }

}

Declare the place counter and data container outside the function:

var place = 0, postData = [];

function submitNotification(cdata){
  place++;
  alert('you selected '+cdata->name+' on '+place+'place');

  postData.push(cdata);

  if (place == 5) {

         $.ajax({ 
    type: 'POST', 
    url: 'http://www.example.com/test.php', 
    data: { places: postData },
    dataType:'json',
    success: function (data) {
        ...
      }
    });

  }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文