通过asp.net中的javascript获取控件ID

时间:2014-07-03 04:51:11

标签: c# javascript asp.net

我的asp.net应用程序中有一个场景,当输入按钮被键入任何多行文本框时,我需要在现有长度上添加一个特定长度并在另一个控件中显示它。我编写了一个通用的javascript函数来处理键入" pnlEnglish"面板。我可以找到"输入键"通过检查" e.keycode == 13" ,但是有可能知道我在哪个控件上发射事件吗?我试图避免将事件添加到文本框,因为我必须处理太多。有没有更好的解决方案来实现这个目标?

<asp:Panel ID="pnlEnglish" runat="server">

 <asp:TextBox ID="txtPlot" runat="server" Rows="5" TextMode="MultiLine" Width="350px" TabIndex="12"></asp:TextBox>
 <asp:TextBox ID="txtLegacyPlot" runat="server" Rows="5" TextMode="MultiLine" Width="350px"TabIndex="12"></asp:TextBox>
 </asp:Panel>

Javascript如下

         $('#<%=pnlEnglish.ClientID%> input:text,#<%=pnlEnglish.ClientID%> textarea').keyup(function (e) {

       if (e.keyCode == 13) {
            //Need to identify the control for which I shall add the length
                        }
            var txtLenPlot = $('#<%=txtPlot.ClientID%>').val().length;
            var txtLenLegacyPlot = $('#<%=txtLegacyPlot.ClientID%>').val().length;


            //The below function shows the length in length displaying control
            update_chars_left(500, $('#<%=txtLengthPlot.ClientID %>'), txtLenPlot);
            update_chars_left(205, $('#<%=txtLengthLegacyPlot.ClientID %>'), txtLenLegacyPlot);

        });

1 个答案:

答案 0 :(得分:1)

在侧键启动功能中,您可以获得文本框的ID,其中按下输入键,如下所示

$('#<%=pnlEnglish.ClientID%> input:text,#<%=pnlEnglish.ClientID%> textarea').keyup(function (e) {

           if (e.keyCode == 13) {
             var currTextboxId =  $(this).attr('id');
            //Your Code

});