我的 asp.net 应用程序中有一个场景,当在任何多行文本框中键入输入按钮时,我需要向现有长度添加特定长度并将其显示在另一个控件中。我编写了一个通用的 javascript 函数来处理“pnlEnglish”面板中的键入。我可以通过检查“e.keycode ==13”找到“Enter key”键,但是有可能知道我从哪个控件触发事件吗?我试图避免向文本框添加事件,因为我必须处理太多事件。有没有更好的解决方案来实现这一目标?
<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);
});
最佳答案
在侧键向上功能中,您可以获取按下 Enter 键的文本框的 ID,如下所示
$('#<%=pnlEnglish.ClientID%> input:text,#<%=pnlEnglish.ClientID%> textarea').keyup(function (e) {
if (e.keyCode == 13) {
var currTextboxId = $(this).attr('id');
//Your Code
});
关于c# - asp.net中通过javascript获取控件Id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24545178/