且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在文本字段中模拟退格动作?

更新时间:2023-01-30 23:07:51

所以我猜你不是把重点放在文本上输入删除它。

So I'm guessing that you don't mean to put focus on the text input to delete it.

有几种方法可以尝试。首先,获取输入的当前内容,并删除最后一个字符,然后将修改后的字符串返回。例如(此代码应该工作):

There are a couple of methods you could try. First, get the current contents of the input and remove the last character, then put the modified string back. For example (this code should work):

var txt = $('#myinputtextbox');
txt.val(txt.val().slice(0, -1));

另一个是使用js来模拟正在被击中的退格键字符。您需要专注于输入,将光标移动到行尾,然后触发该字符。

The other would be to use js to simulate the backspace character key being hit. You would need to focus on the input, move the cursor to the end of the line, and then trigger the character.