2011-12-21 61 views
2

我有一个内容页面有4个不同的选项卡。我使用以下jQuery来控制选项卡。我需要为每个选项卡设置默认按钮,以便当用户单击Enter时,将会触发默认按钮的事件。如何在不同的标签中设置默认按钮?

按键应该做的工作。但不知何故,当我点击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; 
     } 
    }); 
}); 

回答

3

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

相关问题