How to get the raw value an <input type=“number”> field?

前端 未结 4 1568
眼角桃花
眼角桃花 2020-11-22 11:57

How can i get the \"real\" value of an field?


I have an input box, and i\'m using newer HTML5

4条回答
  •  盖世英雄少女心
    2020-11-22 12:58

    Track all pressed keys based on their key codes

    I suppose one could listen to the keyup events and keep an array of all characters entered, based on their keycodes. But it's a pretty tedious task and probably prone to bugs.

    http://unixpapa.com/js/key.html

    Select the input and get the selection as a string

    document.querySelector('input').addEventListener('input', onInput);
    
    function onInput(){
      this.select(); 
      console.log( window.getSelection().toString() )
    }

    All credit to: int32_t

提交回复
热议问题