【问题标题】:how to set default button in different tabs?如何在不同的选项卡中设置默认按钮?
【发布时间】:2011-12-21 20:32:18
【问题描述】:

我有一个包含 4 个不同选项卡的内容页面。我使用以下 jQuery 来控制选项卡。我需要为每个选项卡设置默认按钮,这样当用户点击回车时,就会触发默认按钮的事件。

按键应该可以完成工作。但是不知何故,当我单击 Enter 时,页面被重新加载,而不是被提交。

如何更改我的代码以让页面确定哪个按钮是默认按钮,具体取决于哪个用户控件或选项卡处于活动状态?

$(document).ready(function() {
    var selected_tab = document.getElementById("<%=hfSelectedTab.ClientID %>");
    //When page loads...
    $(".tab_content").hide(); //Hide all content
    if (selected_tab.value == 2) {
        $("ul.tabs li:nth-child(2)").addClass("active").show();
        $(".tab_content:nth-child(2)").show();
    }
    else if (selected_tab.value == 3) {
        $("ul.tabs li:nth-child(3)").addClass("active").show();
        $(".tab_content:nth-child(3)").show();
    }
    else if (selected_tab.value == 4) {
        $("ul.tabs li:nth-child(4)").addClass("active").show();
        $(".tab_content:nth-child(4)").show();
    }
    else {
        $("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 href attribute value to identify the active tab + content
        $(activeTab).fadeIn(); //Fade in the active ID content

        if (activeTab == "#tab1")
            selected_tab.value = 1;
        else if (activeTab == "#tab2")
            selected_tab.value = 2;
        else if (activeTab == "#attachmentcontent")
            selected_tab.value = 3;
        else if (activeTab == "#kb") {
            selected_tab.value = 4;
            document.getElementById("<%=txtRootCause.ClientID %>").focus();
        }

        return false;
    });

    $("form input").keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $('button[type=submit] .default').click();
            return true;
        }
    });
});

【问题讨论】:

    标签: asp.net defaultbutton


    【解决方案1】:

    我的解决方案是将每个选项卡(由 div 定义)包装在一个 asp:Panel 中并设置 Panel DefaultButton 属性。

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 2020-01-08
      • 2021-01-06
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      相关资源
      最近更新 更多