Can I hide the HTML5 number input’s spin box?

后端 未结 17 1262
感动是毒
感动是毒 2020-11-22 05:08

Is there a consistent way across browsers to hide the new spin boxes that some browsers (such as Chrome) render for HTML input of type number? I am looking for a CSS or Jav

相关标签:
17条回答
  • 2020-11-22 05:33

    Maybe change the number input with javascript to text input when you don't want a spinner;

    document.getElementById('myinput').type = 'text';
    

    and stop the user entering text;

      document.getElementById('myinput').onkeydown = function(e) {
      if(!((e.keyCode > 95 && e.keyCode < 106)
        || (e.keyCode > 47 && e.keyCode < 58) 
        || e.keyCode == 8
        || e.keyCode == 9)) {
              return false;
          }
      }
    

    then have the javascript change it back in case you do want a spinner;

    document.getElementById('myinput').type = 'number';
    

    it worked well for my purposes

    0 讨论(0)
  • 2020-11-22 05:40

    Only add this css to remove spinner on input of number

    /* For Firefox */
    
    input[type='number'] {
        -moz-appearance:textfield;
    }
    
    
    
    /* Webkit browsers like Safari and Chrome */
    
    input[type=number]::-webkit-inner-spin-button,
    input[type=number]::-webkit-outer-spin-button {
        -webkit-appearance: none;
        margin: 0;
    }
    
    0 讨论(0)
  • 2020-11-22 05:40

    I've encountered this problem with a input[type="datetime-local"], which is similar to this problem.

    And I've found a way to overcome this kind of problems.

    First, you must turn on chrome's shadow-root feature by "DevTools -> Settings -> General -> Elements -> Show user agent shadow DOM"

    Then you can see all shadowed DOM elements, for example, for <input type="number">, the full element with shadowed DOM is:

    <input type="number">
      <div id="text-field-container" pseudo="-webkit-textfield-decoration-container">
        <div id="editing-view-port">
          <div id="inner-editor"></div>
        </div>
        <div pseudo="-webkit-inner-spin-button" id="spin"></div>
      </div>
    </input>

    And according to these info, you can draft some CSS to hide unwanted elements, just as @Josh said.

    0 讨论(0)
  • 2020-11-22 05:42

    Firefox 29 currently adds support for number elements, so here's a snippet for hiding the spinners in webkit and moz based browsers:

    input[type='number'] {
        -moz-appearance:textfield;
    }
    
    input::-webkit-outer-spin-button,
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }
    <input id="test" type="number">

    0 讨论(0)
  • 2020-11-22 05:44

    Try using input type="tel" instead. It pops up a keyboard with numbers, and it doesn’t show spin boxes. It requires no JavaScript or CSS or plugins or anything else.

    0 讨论(0)
  • 2020-11-22 05:44

    Not what you asked for, but I do this because of a focus bug in WebKit with spinboxes:

    // temporary fix for focus bug with webkit input type=number ui
    if (navigator.userAgent.indexOf("AppleWebKit") > -1 && navigator.userAgent.indexOf("Mobile") == -1)
    {
        var els = document.querySelectorAll("input[type=number]");
        for (var el in els)
            el.type = "text";
    }
    

    It might give you an idea to help with what you need.

    0 讨论(0)
提交回复
热议问题