How can I limit possible inputs in a HTML5 “number” element?

前端 未结 26 2807
感动是毒
感动是毒 2020-11-22 05:12

For element, maxlength is not working. How can I restrict the maxlength for that number element?

26条回答
  •  一整个雨季
    2020-11-22 05:16

    Ugh. It's like someone gave up half way through implementing it and thought no one would notice.

    For whatever reason, the answers above don't use the min and max attributes. This jQuery finishes it up:

        $('input[type="number"]').on('input change keyup paste', function () {
          if (this.min) this.value = Math.max(parseInt(this.min), parseInt(this.value) || 0);
          if (this.max) this.value = Math.min(parseInt(this.max), parseInt(this.value) || 0);
        });
    

    It would probably also work as a named function "oninput" w/o jQuery if your one of those "jQuery-is-the-devil" types.

提交回复
热议问题