简体   繁体   English

将焦点设置在不同的文本框上,因为网页上可以看到不同的div

[英]set focus on different textboxes as different div is visible on a web page

I've a page where on page load event I've set focus on a textbox using javascript. 我有一个页面在页面加载事件中,我已将焦点放在使用JavaScript的文本框中。 But after button press I've to set focus on different text box of different asp:Panel. 但是在按下按钮后,我必须将焦点设置在不同asp:Panel的不同文本框中。 Is this possible using javascript? 是否可以使用javascript? Currently I am doing this via code behind page. 目前,我正在通过页面后面的代码来执行此操作。

<script type="text/javascript">        
        $(function () {
            document.getElementById('<%=txtPassengerId.ClientID%>').focus();
        });
    </script>
<asp:Panel ID = "firstPanel" >
<asp:TextBox ID="txtPassengerId" runat="server"/>
<asp:Button ID="btn_search" runat="server" Text="Search" OnClick="btn_search_Click" />
</asp:Panel>

<asp:Panel ID = "secondPanel" >
<asp:TextBox ID="txtDestination" runat="server"/>
</asp:Panel>

After the button pressed secondPanel is visible and I need to focus on txtDestination text box. 按下按钮后,secondPanel可见,我需要关注txtDestination文本框。 Is this possible using javascript? 是否可以使用javascript?

Yes, you have to use the selector you've used "document.getElementById" and use the .focus() function. 是的,您必须使用选择器“ document.getElementById”,并使用.focus()函数。

document.getElementById("secondpanel").style.visibility = "visible";
document.getElementById("txtDestination").focus();

should do it. 应该这样做。 if you need more just comment. 如果您需要更多,请发表评论。

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

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