Change Input to Upper Case

前端 未结 15 787
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 22:02

JS:



        
15条回答
  •  旧巷少年郎
    2020-12-07 22:25

    Solutions using value.toUpperCase seem to have the problem that typing text into the field resets the cursor position to the end of the text. Solutions using text-transform seem to have the problem that the text submitted to the server is still potentially lowercase. This solution avoids those problems:

    function handleInput(e) {
       var ss = e.target.selectionStart;
       var se = e.target.selectionEnd;
       e.target.value = e.target.value.toUpperCase();
       e.target.selectionStart = ss;
       e.target.selectionEnd = se;
    }

提交回复
热议问题