尝试动态创建 jQuery 单击处理程序时出现问题
我正在使用 API 在我正在制作的网站上创建事件搜索引擎。 API 以 JSON 格式返回数据,我已使用 jQuery 成功将其解析为 HTML。每个事件结果都有一个表单,以便用户可以保存结果以供稍后查看。我的问题是为创建的每个表单创建单独的单击处理程序。目前,每个表单都有自己唯一的 id,但我的 ajax 函数是由提交按钮类调用的,这对于搜索结果中的所有表单来说显然是相同的。我不确定如何将唯一的表单 ID 传递给我的点击函数。
下面的循环是我循环遍历 JSON 结果并将它们作为 HTML 输出的地方。
for (var i = 0, l = events.length; i < l; i++) {
$('.json').append('<div class="gig-listing"><p>'+ events[i].displayName + events[i].location.city + '<br/>' +
'<a href="' + events[i].uri +'" target="_blank" class="gig-link">More info/tickets</a></p><div id="user-preferences-gigs"><ul><?php if ($session->is_logged_in()) { ?><li class="favourite"><form class="gig-search-results" id='+[i]+'><input type="hidden" id="gid" value='+ events[i].id +' /><input type="hidden" id="uid" value="<?php echo $session->user_id; ?>" /><button type="submit" value="Submit" class="gig-favourite-button"><span>Favourite gig</span></button></form></li><?php } ?><li class="share_fb">Share on Facebook</li><li class="default">Explore artist</li></ul></div><br style="clear:both"</div>');
}
我的 ajax 请求的开始是由这个点击处理程序触发的
$('.gig-favourite-button').click(function() {
我的内容我认为我需要做的就是获取所有唯一的表单 id 作为要在此处理程序中调用的实例名称,即如下所示:
$(#'0,#1,#2,#3').click(function() {
我也需要能够删除启动附近搜索时的点击事件,我们将不胜感激
。
I am using an API to create a event search engine on a website I am making. The API returns data in the JSON format and I have successfully parsed this to HTML using jQuery. Each event result has a form in it so the user can save a result to view later. My problem is with creating the individual click handlers for each form that gets created. Currently each form gets its own unique id but my ajax function is called by the submit button class which is obviously the same for all the forms in the search results. What I am unsure of is how to pass the unique form id's to my click function.
The below loop is where I am looping through the JSON results and pumping them out as HTML
for (var i = 0, l = events.length; i < l; i++) {
$('.json').append('<div class="gig-listing"><p>'+ events[i].displayName + events[i].location.city + '<br/>' +
'<a href="' + events[i].uri +'" target="_blank" class="gig-link">More info/tickets</a></p><div id="user-preferences-gigs"><ul><?php if ($session->is_logged_in()) { ?><li class="favourite"><form class="gig-search-results" id='+[i]+'><input type="hidden" id="gid" value='+ events[i].id +' /><input type="hidden" id="uid" value="<?php echo $session->user_id; ?>" /><button type="submit" value="Submit" class="gig-favourite-button"><span>Favourite gig</span></button></form></li><?php } ?><li class="share_fb">Share on Facebook</li><li class="default">Explore artist</li></ul></div><br style="clear:both"</div>');
}
The start of my ajax request is fired by this click handler
$('.gig-favourite-button').click(function() {
What I think I need to do is get all the unique form id as the instance name to call in this handler i.e. like this:
$(#'0,#1,#2,#3').click(function() {
I also need to be able to remove the click events when a near search is initiated. Any help would be greatly appreciated.
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够通过以下方式获取您的 ID。要删除单击事件,您可以使用取消绑定。
You should be able to get to your id in the following way. To remove the click event you can use unbind.
考虑只使用一个,而不是使用多个单击处理程序。例如:
您还可以查看 http://fiddle.jshell.net/Shaz/mZ2eB/
Instead of having multiple click handlers, consider using just one. For example:
You can also take a look at http://fiddle.jshell.net/Shaz/mZ2eB/
如果您尝试对指定类的多个元素进行操作,则需要使用 .each(),如下所示:
但迭代类是较慢的方法之一,因为必须遍历 DOM。如果可能的话,我建议进一步完善选择器。即,如果此类适用于一组选择器,请使用以下内容:
If you are trying to operate on more than one element with the specified class, you need to use .each(), such as this:
But iterating over classes is one of the slower methods, as the DOM has to be walked. I'd suggest further refining the selector if possible. i.e. if this class applies to a set of selectors, use the following: