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

后端 未结 17 1597
感动是毒
感动是毒 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

提交回复
热议问题