尝试动态创建 jQuery 单击处理程序时出现问题

发布于 2024-10-29 22:26:33 字数 1587 浏览 1 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

吃颗糖壮壮胆 2024-11-05 22:26:33

您应该能够通过以下方式获取您的 ID。要删除单击事件,您可以使用取消绑定。

$('.gig-favourite-button').click(function() {
    var id = $(this).siblings("#uid").attr("id");
    // ajax call with id.    
    $(this).unbind("click");
    // or if you want to remove all click events $(".gig-favourite-button").unbind("click");
)};

You should be able to get to your id in the following way. To remove the click event you can use unbind.

$('.gig-favourite-button').click(function() {
    var id = $(this).siblings("#uid").attr("id");
    // ajax call with id.    
    $(this).unbind("click");
    // or if you want to remove all click events $(".gig-favourite-button").unbind("click");
)};
红ご颜醉 2024-11-05 22:26:33

考虑只使用一个,而不是使用多个单击处理程序。例如:

var output = $("#output");

    $("body").click(function(event) {
        event = event || window.event || null;

        if (event) {
            var id = event.target.id,
                className = event.target.className;

            output.html("");

            switch (id) {
            case "test1":
                output.append("Element with the id \"test1\" was clicked<br />");
                break;

            case "test2":
                output.append("Element with the id \"test2\" was clicked<br />");
                break;

            default:
                output.html("No id case<br />");
            }

            switch (className) {
            case "test":
                output.append("Element with the class \"test\" was clicked<br />");
                break;

            default:
                output.append("No class case.<br />");
            }
        }
    });

您还可以查看 http://fiddle.jshell.net/Shaz/mZ2eB/

Instead of having multiple click handlers, consider using just one. For example:

var output = $("#output");

    $("body").click(function(event) {
        event = event || window.event || null;

        if (event) {
            var id = event.target.id,
                className = event.target.className;

            output.html("");

            switch (id) {
            case "test1":
                output.append("Element with the id \"test1\" was clicked<br />");
                break;

            case "test2":
                output.append("Element with the id \"test2\" was clicked<br />");
                break;

            default:
                output.html("No id case<br />");
            }

            switch (className) {
            case "test":
                output.append("Element with the class \"test\" was clicked<br />");
                break;

            default:
                output.append("No class case.<br />");
            }
        }
    });

You can also take a look at http://fiddle.jshell.net/Shaz/mZ2eB/

倾城花音 2024-11-05 22:26:33

如果您尝试对指定类的多个元素进行操作,则需要使用 .each(),如下所示:

$('.gig-favourite-button').each(function() {
}

但迭代类是较慢的方法之一,因为必须遍历 DOM。如果可能的话,我建议进一步完善选择器。即,如果此类适用于一组选择器,请使用以下内容:

$('select.gig-favourite-button').each(function() {
}

If you are trying to operate on more than one element with the specified class, you need to use .each(), such as this:

$('.gig-favourite-button').each(function() {
}

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:

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