文本框仅可接收decimal

来源:互联网 发布:c语言第四版答案完整版 编辑:程序博客网 时间:2024/06/03 17:42

文本框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

 

结果:

 

0 0
原创粉丝点击