按下删除键时,如何判断输入框内 内容是否为空

我的目的是当输入框内 内容为空时,再按下删除键,执行我需要的操作


if (e.target.value === '') {

            if (e.keyCode === 8) {

                //todo

            }

        }

如果我像这样判断的话,当输入框内还剩下最后一个字符时,也会执行todo,请问有什么更好的解决方法吗


噜噜哒
浏览 466回答 1
1回答

绝地无双

&nbsp; &nbsp; <input type="text" onkeydown="aaa(event)">&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; function aaa(event){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var e = event;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.target.value === '') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (e.keyCode === 8) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log('111');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript