Home  >  Article  >  Web Front-end  >  Modify jQuery.Autocomplete plug-in to support Chinese input method to avoid TAB and ENTER key failure and cause form submission_jquery

Modify jQuery.Autocomplete plug-in to support Chinese input method to avoid TAB and ENTER key failure and cause form submission_jquery

WBOY
WBOYOriginal
2016-05-16 18:45:041319browse

But the only regret is that when the Chinese input method is turned on, firefox3.0 automatically matches Chinese pinyin, but cannot trigger matching in time for input Chinese; while under my IE6.0, there is no such problem. .
Cause analysis:
The Autocomplete plug-in triggers automatic matching of user input characters through the "keydown" event (line 92 of jquery.autocomplete.js can be analyzed). In IE6, when the input method is opened, The entered characters will not trigger "keydown". It will only be triggered after the Chinese input is completed, so there is no difference between Chinese input and Latin input; but under Firefox 3.0, no matter whether the input method is turned on or not, the key will trigger the "keydown" event. , so after the Chinese input is completed, some of the Chinese pinyin letters just typed are automatically matched. ------So only Firefox has a problem.
Solution:
The most common method found on the Internet is to modify line 92 of jquery.autocomplete.js and replace "keydown" with "keyup", but this is not the fundamental solution, although it can be used in firefox after this modification. Automatically match the input Chinese in a timely manner, but the important event mechanisms such as carriage return and tab in the original plug-in are destroyed. For example, after this change, if your input is in a form, carriage return will be selected from the original one. Inputting items into the input changes to directly submitting the form, which is not what we want.
The principle of my method is to add an event that the original plug-in triggers the query, that is, when the characters in the input input field change, the query is re-queried (calling its internal onChange function). This is mainly for firefox, because our The most accessed systems are IE and Firefox. Firefox happens to have an input change event called oninput, so we only need to insert the following code in line 199 of the original jquery.autocomplete.js:

Copy code The code is as follows:

.bind("input", function() {
// @hack:support for inputing chinese characters in firefox
onChange(0, true);
});

After insertion, the code is as follows:
Copy code The code is as follows:

...
...
jQueryinput.unbind();
jQuery(input.form).unbind(".autocomplete");
}).bind("input", function() {
// @hack:support for inputing chinese characters in firefox
onChange(0, true);
});
. ..
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn