所以基本上我有几个文本框,我只想接受其中的数字,但是我无法让下面使用的 JQuery Im 与 ListView 内的文本框或另一个 asp.net 页面一起使用。
$(document).ready(function () {
$("#<%= txtSearch.ClientID %>").keydown(function (event) {
// Allow: backspace, delete, tab, escape, and enter
if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 ||
// Allow: Ctrl+A
(event.keyCode == 65 && event.ctrlKey === true) ||
// Allow: home, end, left, right
(event.keyCode >= 35 && event.keyCode <= 39)) {
// let it happen, don't do anything
return;
}
else {
// Ensure that it is a number and stop the keypress
if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105)) {
event.preventDefault();
}
}
});
});
此代码适用于名为 txtSearch 的普通文本框,但是当我在 txtSearch 中插入 txtQty 时,它不起作用:s
<asp:ListView ID="lvProducts" runat="server" OnItemCommand="lbProducts_ItemCommand" OnSelectedIndexChanged="lvProducts_SelectedIndexChanged">
<LayoutTemplate>
<div class="productoutercontainer">
<div id="itemPlaceHolder" runat="server"></div>
</div>
</LayoutTemplate>
<ItemTemplate>
<div class="productinnercontainer">
<img class="pics" src="<%#Eval("ProductImage") %>" alt="<%#Eval("ProductName") %>" title="<%#Eval("ProductName") %>" />
<asp:Label ID="lblName" runat="server" Text='<% # Eval("ProductName") %>' Font-Bold="true" Font-Size="Medium"></asp:Label><br /><br />
Quantity: <asp:TextBox ID="txtQty" runat="server" columns="3"/>
<asp:Button ID="btnAddToCart" runat="server" Text="Add To Cart" CssClass="button2" CommandArgument='<%# Eval("ProductId")%>' OnClick="btnAddToCart_Click" /> <br /><br /><br />
</div>
</ItemTemplate>
</asp:ListView>