ASP.NET 会自动生成一个 ID,而不是使用我指定的 ID。我将如何使用 jQuery 选择此值?我不相信自动生成的值会始终相同。
这是我的 ASPX 代码:
<asp:TextBox ID="txtZip" runat="server" CausesValidation="true" CssClass="short_width" MaxLength="10" />
这是生成的内容:
<input name="ctl00$MainContentArea$txtZip" type="text" maxlength="10" id="ctl00_MainContentArea_txtZip" class="short_width" />
这是我正在尝试做的事情:
<script type="text/javascript">
jQuery(function ($) {
$("#txtZip").mask("99999?-9999");
});
</script>
最佳答案
您可以使用类选择器:
jQuery(function ($) {
$(".short_width").mask("99999?-9999");
});
我会创建一个新类,即 zipCode,并将其添加到您的控件中:
<asp:TextBox ID="txtZip" runat="server" CausesValidation="true" CssClass="short_width zipCode" MaxLength="10" />
然后使用这个:
jQuery(function ($) {
$(".zipCode").mask("99999?-9999");
});
这样它就可以将它与您的其他样式分开。
关于c# - 使用 JQuery 选择自动生成的 WebForm 控件 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244407/