JavaScript显示最后一个选项卡

JavaScript显示最后一个选项卡,javascript,jquery,html,Javascript,Jquery,Html,好的,我已经创建了4个选项卡,一次只显示1个,但是如果我在选项卡4的表单中发布某个内容,那么选项卡1将变为活动状态,但我希望它自动显示发布内容的选项卡。抱歉,如果描述不正确,但这是我的代码: <script src="../js/jquery-1.8.2.min.js" type="text/javascript"></script> <script type="text/javascript"> $(document).ready(function() {

好的,我已经创建了4个选项卡,一次只显示1个,但是如果我在选项卡4的表单中发布某个内容,那么选项卡1将变为活动状态,但我希望它自动显示发布内容的选项卡。抱歉,如果描述不正确,但这是我的代码:

<script src="../js/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function() {

    //Default Action
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li:first").addClass("active").show(); //Activate first tab
    $(".tab_content:first").show(); //Show first tab content

    //On Click Event
    $("ul.tabs li").click(function() {
        $("ul.tabs li").removeClass("active"); //Remove any "active" class
        $(this).addClass("active"); //Add "active" class to selected tab
        $(".tab_content").hide(); //Hide all tab content
        var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active content
        return false;
    });

});
</script>


<center>
  <ul class="tabs">
    <li><a href="#tab1">Tab 1</a></li>
    <li><a href="#tab2">Tab 2</a></li>
    <li><a href="#tab3">Tab 3</a></li>
    <li><a href="#tab4">Tab 4</a></li>

  </ul>
  <div class="tab_container"><br><br><br>
    <div id="tab1" class="tab_content">
<center><h2>Tab 1</h2></center>
                </div>
                    <div id="tab2" class="tab_content">
<center><h2>Tab 2</h2></center>
                </div>
                    <div id="tab3" class="tab_content">
<center><h2>Tab 3</h2></center>
                </div>
                    <div id="tab4" class="tab_content">
                    <?php
                    $getmessage = $_POST["message"];
                    if($getmessage != null) {
                        echo"Thanks for posting the text!";
                    } ?>
<center><form method="post" name="form4"><input type="text" id="form4" name="message" placeholder="Type a text here"><br><input type="submit" value="Post text"></form></center>
                </div>
</div></center>
</center>

$(文档).ready(函数(){
//默认动作
$(“.tab_content”).hide();//隐藏所有内容
$(“ul.tabs li:first”).addClass(“active”).show();//激活第一个选项卡
$(“.tab_content:first”).show();//显示第一个选项卡内容
//点击事件
$(“ul.tabs li”)。单击(函数(){
$(“ul.tabs li”).removeClass(“active”);//删除任何“active”类
$(this).addClass(“active”);//将“active”类添加到所选选项卡
$(“.tab_content”).hide();//隐藏所有选项卡内容
var activeTab=$(this).find(“a”).attr(“href”);//查找rel属性值以标识活动选项卡+内容
$(activeTab).fadeIn();//淡入活动内容
返回false;
});
});



表1 表2 表3

因此,当我在选项卡4中提交表单时,我进入选项卡1,我必须手动进入选项卡4以查看echo显示的消息。我需要它自动转到提交表单的选项卡,但我不知道如何操作。

给您的表单
操作=“#tab4”
(确保重新加载页面)。此外,在JS脚本中,您还必须添加要从url读取的代码:

var hash = (window.location.hash).substr(1) || null;
if(hash == "tab4")
    $(".tab_content:last").show();
在表单中添加另一个输入(隐藏)元素,例如
active\u tab
,并在单击选项卡时用活动选项卡填充它。从
$\u POST
中获取该值,并相应地设置默认选项卡

步骤1:添加输入元素

步骤3:根据post值设置默认页签

$(文档).ready(函数(){
//默认动作
var tabNum=“”;
$(“.tab_content”).hide();//隐藏所有内容
$(“ul.tabs li”).eq(tabNum).addClass(“active”).show();//激活选项卡
$(“.tab_content”).eq(tabNum.show();//显示选项卡内容

提交后,重定向回该页面,url末尾带有#tab4。您可以使用ajax提交帖子,然后避免重新加载页面,或者在返回时发送一个参数,检查是否来自帖子,然后设置所需的选项卡。
<input type="hidden" name="active_tab" />
$("ul.tabs li").click(function() {
    $('input[name="active_tab"]').val( $(this).index() );
    //your remaining code as is
    //...
$(document).ready(function() {
    //Default Action
    var tabNum = "<? echo !empty($_POST['active_tab']) ? $_POST['active_tab'] : 0; ?>";
    $(".tab_content").hide(); //Hide all content
    $("ul.tabs li").eq(tabNum).addClass("active").show(); //Activate the tab
    $(".tab_content").eq(tabNum).show(); //Show the tab content