开发者

jquery switch statement and scope issue

开发者 https://www.devze.com 2022-12-07 23:55 出处:网络
I have this working script (i know its incomplete): function rsvpNew(selector,function_url) { $(selector).livequery(\'click\',function(){

I have this working script (i know its incomplete):

function rsvpNew(selector,function_url) {
 $(selector).livequery('click',function(){
   var select = $(selector).attr('rel');
   var event_id = $(this).parents(开发者_如何学Python'ul.event-options').attr('rel');
   element = this;

   switch(select) {
        case "attending":
        $.ajax({
        type: "POST",
        url: "/events/set_member/"+function_url,
        data: "event_id="+event_id,
        beforeSend:  function() {
        $("<span class='notice'>Saving...</span>").prependTo('body');
                        },
        complete: function() {


                $('span.notice').fadeOut(500);
                $(element).closest('span.rsvp-status').html("I'm Attending &ndash; <a href='javascript:;' class='remove' rel='remove'>Remove</a>");
                }


        });


             break;

Now the line

$(element).closest('span.rsvp-status').html("I'm Attending &ndash; <a href='javascript:;' class='remove' rel='remove'>Remove</a>");

Is working, but when I do something similar to get the parent ul of this li, it doesn't work. Any ideas?


but when I do something similar to get the parent ul of this li, it doesn't work. Any ideas?

To get the parent of something you can use a few options:

$('li').parent('ul'); //will return your ul

Gets the direct parent of an element.

Another way is to use closest:

$('li').closest('ul'); //should also return your ul

Gets a set of elements containing the closest parent element that matches the specified selector. Especially useful for event delegation.

0

精彩评论

暂无评论...
验证码 换一张
取 消