如何使用 javascript / jquery 增加点击次数?
我有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在函数外部声明位置计数器和数据容器:
Declare the place counter and data container outside the function: