简体   繁体   English

表单提交后,jQuery ui选项卡仍保留在选项卡上

[英]jquery ui tabs stay on tab after form submit

Hi I'm using jQuery ui tabs Widget in a page with two tabs. 嗨,我正在使用带有两个标签的页面中的jQuery ui标签小部件。 Each page contains one form with the same page as an action. 每个页面包含一个表单,该表单具有与动作相同的页面。 I need to stay in the tab where the form was submitted after the form is submitted. 提交表单后,我需要留在提交表单的标签中。 How can I accomplish that?. 我该怎么做?

Here's my code: 这是我的代码:

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Tab 1</a></li>
    <li><a href="#tabs-2">Tab 2</a></li>
  </ul>
  <div id="tabs-1">
    <p>I'm Tab1.</p>
       <p>I'm a Form using GET Method, passing values to this same page:<br></p>
       <p><form name="input" action="index.php" method="GET">
       <input type="text" name="value" id="somevalue" style="width:150px;"> <br>
       </form></p>
    <?
    if (isset($_GET)) {echo ($_GET['value']);}
    ?>
  </div>
  <div id="tabs-2">
    <p>I'm Tab2.</p>
     <p>I'm a Form using GET Method, passing values to this same page:<br></p>
       <p><form name="input" action="index.php" method="GET">
       <input type="text" name="value2" id="somevalue" style="width:150px;"> <br>
       </form></p>
    <?
       if (isset($_GET)) {echo ($_GET['value2']);}
    ?>
  </div>

</div>

Thanks in advance. 提前致谢。 Sebastian 塞巴斯蒂安

You can achieve this by using jQuery.cookie plugin. 您可以使用jQuery.cookie插件来实现。

You need to save selected tab index to cookies and on each load of page read those cookie and select appropriate tab. 您需要将选定的选项卡索引保存到cookie中,并在每次页面加载时读取这些cookie并选择适当的选项卡。 Saving is made inside activate handler of tabs plugin (value of the active option will be saved) 在tabs插件的activate处理程序中进行保存(将保存active选项的值)

$("#tabs").tabs({
    activate: function(event, ui){
        var active = $("#tabs").tabs("option", "active");
        $.cookie("activeTabIndex", active);
    }
});

To select needed tab at page load you need to read the cookie and set the active option on tabs. 要在页面加载时选择所需的标签,您需要阅读cookie并在标签上设置active选项。 Here is the full $(document).ready code: 这是完整的$(document).ready代码:

$(document).ready(function(){
    //initialize tabs plugin with listening on activate event
    var tabs = $("#tabs").tabs({
        activate: function(event, ui){
            //get the active tab index
            var active = $("#tabs").tabs("option", "active");

            //save it to cookies
            $.cookie("activeTabIndex", active);
        }
    });

    //read the cookie
    var activeTabIndex = $.cookie("activeTabIndex");

    //make active needed tab
    if(activeTabIndex !== undefined) {
        tabs.tabs("option", "active", activeTabIndex);
    }
});

Here is the Demo 这是演示

(i removed the form s to make it work in jSFiddle; if you are staying on the same page after submitting your form , it will work) (我删除了form s使其在jSFiddle中工作;如果您在提交form后停留在同一页面上,它将起作用)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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