zoukankan      html  css  js  c++  java
  • 文本框仅可接收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

    结果:

  • 相关阅读:
    day89
    day88
    day87
    day86
    day85
    day84
    day83
    Maven仓库汇总
    [转载]AngularJS入门教程04:双向绑定
    [转载]AngularJS入门教程03:迭代器
  • 原文地址:https://www.cnblogs.com/insus/p/6500195.html
Copyright © 2011-2022 走看看