本文介绍了如何在文本字段中模拟退格动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当按下按钮时,我只想在常规文本框中模拟退格,删除光标左侧的字符。

When a button is pressed, I simply want to simulate a "backspace" in a regular text box, deleting the character to the left of the cursor.

Google并且各种论坛正在产生非常随机的结果。这样做的正确方法是什么?

Google and various forums are producing really random results on this. What's the proper way to do this?

推荐答案

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

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.

这篇关于如何在文本字段中模拟退格动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 22:18