如何将jqury用于多个文本框以仅限制数字 [英] How to use jqury for multiple text boxes to restrict only numbers

查看:122
本文介绍了如何将jqury用于多个文本框以仅限制数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-场景

-现在以下代码可以正常工作.但是页面中有两个文本框.但是我不想为other(2nd)文本框编写以下代码.是否可以在下面的代码中在此处添加othertextbox(2nd)id. ?


--Scenario

--Now the below code is working fine. But there are two textbox in the page. But i dont want to write the below code for the other(2nd) textbox. Is it possible to add the othertextbox(2nd) id here in the below code. ?


<script type="text/javascript">
   $(function() {
   $('[id*=txtdistanceops]').keydown(function(e) {
           if (e.shiftKey || e.ctrlKey || e.altKey) {
               e.preventDefault();
           } else {
               var key = e.keyCode;
               if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105)))

               {

                   e.preventDefault();

                   alert("Please enter numeric value!");

                   $('[id*=txtdistanceops]').val('');



               }

           }

       });

   });

   </script>

   <script type="text/javascript">





--aspx页面





--aspx page

<asp:TemplateField HeaderText="Approved Km By Ops">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistanceops" MaxLength="6" CssClass="textBox_for6tds" runat="server"

                            Text='<%#Bind("distanceByOps") %>'>  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Approved Km By CTV" Visible="false">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistancectv" MaxLength="6" CssClass="textBox_for6tds" runat="server">  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>




请指导我.




Please guide me.

推荐答案

(function(){
(function() {


('[id * = txtdistanceops]').keydown(功能(e){ 如果(e.shiftKey || e.ctrlKey || e.altKey){ e.preventDefault(); } 别的 { var key = e.keyCode; if(!((key == 8)||(key == 46)||(key> = 35&& key < = 40) > = 48&&键< = 57) || (key > = 96&&键< = 105))) span> { span> 输入 数字 值!); span>
('[id*=txtdistanceops]').keydown(function(e) { if (e.shiftKey || e.ctrlKey || e.altKey) { e.preventDefault(); } else { var key = e.keyCode; if (!((key == 8) || (key == 46) || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105))) { e.preventDefault(); alert("Please enter numeric value!");


('[id * = txtdistanceops]' ).val(''); span> } span> } span> }); }); </script > < 脚本 =" 文本/javascript"
('[id*=txtdistanceops]').val(''); } } }); }); </script> <script type="text/javascript">





--aspx页面





--aspx page

<asp:TemplateField HeaderText="Approved Km By Ops">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistanceops" MaxLength="6" CssClass="textBox_for6tds" runat="server"

                            Text='<%#Bind("distanceByOps") %>'>  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Approved Km By CTV" Visible="false">
                    <ItemTemplate>
                        <asp:TextBox ID="txtdistancectv" MaxLength="6" CssClass="textBox_for6tds" runat="server">  </asp:TextBox>
                    </ItemTemplate>
                    <ItemStyle HorizontalAlign="center" />
                </asp:TemplateField>




请引导我.




Please guide me.


这篇关于如何将jqury用于多个文本框以仅限制数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆