Chrome and jQuery issue
This is weird but I have some autoco开发者_Python百科mplete code that I've customized to only engage when you type the '@' sign... In FireFox it works great. I type one '@' and the dropdown appears.
BUT... when I try it in Chrome, it requires two '@@' to engage the dropdown.
What could this be?
Here is the code in a switch statement that activates the autocomplete dropdown:
case KEY.ATSIGN:
clearTimeout(timeout);
timeout = setTimeout(onChange, options.delay);
//alert("hi");
//select.show();
break;
default:
break;
Another strange behavior is that when ad an alert statment, it actually works when I type one '@' in Chrome... Any ideas on how to fix this? Thanks!
EDIT
Here's the onChange function where I am replacing the @ sign with blank "" so it doesn't show or submit the @ when an item is selected from the dropdown
function onChange(crap, skipPrevCheck) {
if( lastKeyPressCode == KEY.DEL ) {
select.hide();
return;
}
var currentValue = $input.val();
if ( !skipPrevCheck && currentValue == previousValue )
return;
previousValue = currentValue;
currentValue = lastWord(currentValue);
if ( currentValue.length >= options.minChars) {
$input.addClass(options.loadingClass);
if (!options.matchCase)
currentValue = currentValue.toLowerCase();
currentValue = currentValue.replace("@","");
request(currentValue, receiveData, hideResultsNow);
//alert(currentValue);
} else {
stopLoading();
select.hide();
}
};
NEW EDIT You can see the at sign ascii value is set here as 50
var KEY = {
UP: 38,
DOWN: 40,
DEL: 46,
TAB: 9,
RETURN: 13,
ESC: 27,
COMMA: 188,
PAGEUP: 33,
PAGEDOWN: 34,
BACKSPACE: 8,
ATSIGN: 50
};
Then in the switch statement, the autocomplete gets activated only when the @ sign is pressed:
switch(event.keyCode) {
case KEY.UP:
event.preventDefault();
if ( select.visible() ) {
select.prev();
} else {
onChange(0, true);
}
break;
case KEY.DOWN:
event.preventDefault();
if ( select.visible() ) {
select.next();
} else {
onChange(0, true);
}
break;
case KEY.PAGEUP:
event.preventDefault();
if ( select.visible() ) {
select.pageUp();
} else {
onChange(0, true);
}
break;
case KEY.PAGEDOWN:
event.preventDefault();
if ( select.visible() ) {
select.pageDown();
} else {
onChange(0, true);
}
break;
// matches also semicolon
case options.multiple && $.trim(options.multipleSeparator) == "," && KEY.COMMA:
case KEY.TAB:
case KEY.RETURN:
if( selectCurrent() ) {
// stop default to prevent a form submit, Opera needs special handling
event.preventDefault();
blockSubmit = true;
return false;
}
break;
case KEY.ESC:
select.hide();
break;
case KEY.ATSIGN:
clearTimeout(timeout);
timeout = setTimeout(onChange, options.delay);
//alert("hi");
//select.show();
break;
default:
break;
}
And here is the function onChange that gets called in the case statement after @ is pressed
function onChange(crap, skipPrevCheck) {
if( lastKeyPressCode == KEY.DEL ) {
select.hide();
return;
}
var currentValue = $input.val();
if ( !skipPrevCheck && currentValue == previousValue )
return;
previousValue = currentValue;
//alert(previousValue);
currentValue = lastWord(currentValue);
if ( currentValue.length >= options.minChars) {
$input.addClass(options.loadingClass);
if (!options.matchCase)
currentValue = currentValue.toLowerCase();
currentValue = currentValue.replace("@","");
//alert(currentValue);
request(currentValue, receiveData, hideResultsNow);
} else {
stopLoading();
select.hide();
}
};
As I mentioned, this works great in FF... I press @ once and the autocomplete activates... However in Chrome, I must press @@ (at sign twice)....
Also here is a bit of code right before the switch statement
// only opera doesn't trigger keydown multiple times while pressed, others don't work with keypress at all
$input.bind(($.browser.opera ? "keypress" : "keydown") + ".autocomplete", function(event) {
// a keypress means the input has focus
// avoids issue where input had focus before the autocomplete was applied
hasFocus = 1;
// track last key pressed
lastKeyPressCode = event.keyCode;
Look at the 3rd comment on the question.
jQuery Event Keypress: Which key was pressed?
you might want to try using keyup instead of keypress and see how far you get. Also, if you are posting a problem it's best to strip out all the non-relevant code and reduce your problem to the bare minimum.
you might also want to mock up a quick example at something like http://jsbin.com/
精彩评论