简体   繁体   English

使用JavaScript代码在文本框上触发退格事件

[英]Fire backspace event on textbox using javascript code

Is it possible to fire backspace event on textbox using javascript? 是否可以使用JavaScript在文本框上触发退格事件? Which should clear the character left to the cursor position. 应该清除光标位置左侧的字符。 I know that event code for backspace is 8 but I don't know how to fire it using the code. 我知道退格事件代码为8,但我不知道如何使用代码触发事件。

Solution specific to Internet Explorer 8 would be of great help. 特定于Internet Explorer 8的解决方案将有很大帮助。

Have a look at this related question: Simulate JavaScript Key Events 看看这个相关的问题: 模拟JavaScript按键事件

Personally, I think it would be a better, cleaner approach to edit the text directly, removing the char before the cursor position. 我个人认为,直接编辑文本(在光标位置之前删除字符)是一种更好,更简洁的方法。 See How to get caret position in textarea 请参见如何在文本区域中获得插入符位置

Would be a lot easier to simply trim the value by one character from the end.. 从末尾简单地将值修剪一个字符会容易得多。

var TheTextBox = document.getElementById("id");
TheTextBox.value = TheTextBox.value.substring(0, TheTextBox.length - 1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM