文本框仅可接收decimal

文本框html如下:

 

<div><label class="label">价格:</label><input id="TextPrice" type="text" name="Price" /></div>
Source Code


jQuery:

 

 $("#TextPrice").on("input", function (evt) {
            var self = $(this);
            self.val(self.val().replace(/[^0-9\.]/g, ''));
            if ((evt.which != 46 || self.val().indexOf('.') != -1) && (evt.which < 48 || evt.which > 57)) {
                evt.preventDefault();
            }
        });
View Code

 

如果此文本框只接受整数呢,可以把jQuery稍修改一下:

 

$("#TextPrice").on("input", function (evt) {
            var self = $(this);
            self.val(self.val().replace(/[^\d].+/, ""));
            if ((evt.which < 48 || evt.which > 57)) {
                evt.preventDefault();
            }
        });
Source Code

 

结果:

 

posted @ 2017-03-04 07:40  Insus.NET  阅读(446)  评论(0编辑  收藏  举报