In JavaScript, how do I handle backspace and delete key presses to perform its key function as well as extra stuff?
I have a text box where I would like to 开发者_C百科be able to press the backspace or delete key in it, and have these key presses perform their relevant function, i.e., have the character next to the cursor removed, but in addition I would like extra stuff to be done on those keep press events as well.
My solution so far involved calling a function that checks for the backspace or delete keyCode on the onKeyDown event, but the problem is once it handles the extra stuff I want done associated with those key presses, it does not actually perform the key functionality of removing the characters in the text box. A code example is shown below. This gives the effect that the keys don't work to the user when they are making edits in the text box.
function foo(obj)
{
// if key is backspace or delete ...
if ((event.keyCode == 8 || event.keyCode == 46))
{
// do extra stuff here.
}
}
I was wondering if there is a way of triggering the backspace or delete key functionality without having to determining the cursor position and manipulating the strings in the text box (and not involving jQuery in solution)?
It seems that I was calling an existing function, written by someone else, and in that function it had the following code in it:
event.returnValue = false;
Apparently, this property seems to prohibit the default action connected to the event.
精彩评论