简体   繁体   中英

JQuery Tabs , can´t change activate tab

i'm new here and didn´t find the solution for my problem. i searched the web and tried tons of solutions but they didn´t work.

i use Jquery Tabs with the closable feature( http://jsfiddle.net/42er3/9/ )

Now i wanted to activate the latest created Tab. This doesn´t work ^^

Here is my Code:

$(function () {
    var tabTitle = $("#tab_title"),
        tabContent = $("#tab_content"),
        tabTemplate = "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span></li>",
        tabCounter = 1;
    var tabs = $("#tabs").tabs();

    // modal dialog init: custom buttons and a "close" callback resetting the form inside
    var dialog = $("#dialog").dialog({
        autoOpen: false,
        modal: true,
        buttons: {
            Send: function () {
                addTab();
                $(this).dialog("close");
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            form[0].reset();
        }
    });
    // addTab form: calls addTab function on submit and closes the dialog
    var form = dialog.find("form").submit(function (event) {
        addTab();
        dialog.dialog("close");
        event.preventDefault();
    });

    // actual addTab function: adds new tab using the input from the form above
    function addTab() {
        var label = tabTitle.val() || "Tab " + tabCounter,
            id = tabTitle.val(),
            li = $(tabTemplate.replace(/#\{href\}/g, "#" + id).replace(/#\{label\}/g, label)),
            tabContentHtml = tabContent.val() || "Tab " + tabCounter + " content.";

        tabs.find(".ui-tabs-nav").append(li);

        tabs.append("<div id='" + id + "' class='rel'><p>" + tabContentHtml + "</p><hr /><div id='writebox'><form name='send' id ='sent'><textarea name ='msg' class='boxsizingBorder'></textarea><input type='submit' name='" + id + "' /></form></div></div>");

        //after added the tab, active it
        tabs.tabs({
            active: tabCounter
        }); // <---------------------------Here it is

        //alert(panelId+"-"+tabCounter);
        tabCounter++;
        tabs.tabs("refresh");

    }
    // addTab button: just opens the dialog
    $("#add_tab")
        .button()
        .click(function () {
            dialog.dialog("open");
        });
    // close icon: removing the tab on click
    tabs.delegate("span.ui-icon-close", "click", function () {
        var panelId = $(this).closest("li").remove().attr("aria-controls");
        $("#" + panelId).remove();
        tabs.tabs("refresh");
    });
    tabs.bind("keyup", function (event) {
        if (event.altKey && event.keyCode === $.ui.keyCode.BACKSPACE) {
            var panelId = tabs.find(".ui-tabs-active").remove().attr("aria-controls");
            $("#" + panelId).remove();
            tabs.tabs("refresh");
        }
    });
});

You can use the following code to programatically make a tab active

$( "#tabs" ).tabs( "option", "active", 2 );

I've updated the addTab() method in your Fiddle as follows:

function addTab() {
    ...
    tabs.tabs("refresh");
    tabs.tabs( "option", "active", tabCounter-1 );
    tabCounter++;
}

As tab indexes start at 0 rather than 1, I've had to subtract 1 from tabCounter to get the index of the tab that was just added.

See here for a demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM