javascript - JQuery UI: Accordion callbacks - Stack Overflow

I need my javascript to only do the callback when I OPEN a section on the accordion, as of right now it

I need my javascript to only do the callback when I OPEN a section on the accordion, as of right now it does a callback when I open OR close a section because I'm only using a click function. Is there a way I can modify my existing click function to only run when the given section is activated?

My current click function:

$("a#mimetypes").click(function() {
    $("span#mimetypesthrobber").loading(true, { max: 1500 })
    $.getJSON("../mimetypes", function(data) {
        //callback
    });
});

Thanks!

EDIT:

I already tried this with another part of the accordion and it wasn't working properly:

$('.ui-accordion').bind('accordionchange', function(event, ui) {
if (ui.newHeader == "Encoders") {
EncodersGet();
}
});

I need my javascript to only do the callback when I OPEN a section on the accordion, as of right now it does a callback when I open OR close a section because I'm only using a click function. Is there a way I can modify my existing click function to only run when the given section is activated?

My current click function:

$("a#mimetypes").click(function() {
    $("span#mimetypesthrobber").loading(true, { max: 1500 })
    $.getJSON("../mimetypes", function(data) {
        //callback
    });
});

Thanks!

EDIT:

I already tried this with another part of the accordion and it wasn't working properly:

$('.ui-accordion').bind('accordionchange', function(event, ui) {
if (ui.newHeader == "Encoders") {
EncodersGet();
}
});
Share Improve this question edited Mar 24, 2010 at 14:23 user177215 asked Mar 24, 2010 at 13:54 user177215user177215 2492 gold badges5 silver badges11 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

you can use the the "change event"

$('.ui-accordion').bind('accordionchange', function(event, ui) {
  ui.newHeader // jQuery object, activated header
  ui.oldHeader // jQuery object, previous header
  ui.newContent // jQuery object, activated content
  ui.oldContent // jQuery object, previous content
});

and access the "newHeadert" for example and do your processing

EDIT

according to the new info {collapsible: true, active: false}

$(document).ready(function() {
            var $acc = $('#accordion').accordion({ collapsible: true,
                   active : false ,
                   change : function (event, ui)
                   {
                                var index = $acc.accordion( "option", "active");
                    if( index === false){
                                 // all are close
                                }
                                else{
                                 // 0-based index of the open section
                                }

                   }
            });
        });

the "option, active" would return you the index of the open section or "false" if all sections are closed

One improvement on undertakerors answer: use triple equals when paring index to false to avoid the first accordion element to match.

if (index === false) {
  // All are closed
} else {
  // 0-based index of the open section
}

Please remember that double equals will perform type conversion when evaluating conditions.

If all the accordion sections are closed by dfault you could replace the click event with toggle and have the second function simple do nothing.

$("a#mimetypes").toggle(function() {
    $("span#mimetypesthrobber").loading(true, { max: 1500 });
    $.getJSON("../mimetypes", function(data) {
        //callback 
    });
},
function() {
    //do nothing
});

The better solution would be to add a class to the active section and check for that class before calling the load.

$("a#mimetypes").click(function() {
    if ($(this).hasClass("active")) {
        $(this).removeClass("active");
    }
    else {
        $(".active").removeClass("active"); //Edit - remove all active classes to account for this section being closed by the opening of another
        $(this).addclass("active");

        $("span#mimetypesthrobber").loading(true, { max: 1500 });
        $.getJSON("../mimetypes", function(data) {
            //callback 
        });
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745614048a4636122.html

相关推荐

  • javascript - JQuery UI: Accordion callbacks - Stack Overflow

    I need my javascript to only do the callback when I OPEN a section on the accordion, as of right now it

    6小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信