问题
I have an input form that has a jQuery event applied to it that creates square brackets around the users input. The issue is that the event prevents the ability to use backspace to delete the text. I'm looking for a way to check if backspace is being pressed and prevent the code from runnning.
jQuery Script
function test(obj) {
var oldVal = obj.val().replace(/\[/g,\'\');
oldVal = oldVal.replace(/\]/g,\'\');
var val = \'[\' + oldVal + \']\';
obj.val(val);
Input Form
oninput="test($(this));"
回答1:
function test(obj,e) {
if(!(e.which == 8)){
var oldVal = obj.val().replace(/\[/g,'');
oldVal = oldVal.replace(/\]/g,'');
var val = '[' + oldVal + ']';
obj.val(val);
}
}
and pass event as parameter ..
<input class="text-box" onkeyup="test($(this),event)">
来源:https://stackoverflow.com/questions/25310701/enable-backspace-with-jquery-oninput-event-within-the-text-on-an-input-form