JavaScript I / O Prediction

ive got a text input through which someone can start typing, and then under the list it will be filled in according to what matches your input.

1) Currently im using this to check for 'contains':

if(listToRedo[i].value.indexOf(text) != -1){

but the problem is that it seems to be case sensitive, how can I change this?

2) Do onkeyevent, onkeypress, onkeydown do the key removal action? In addition, which one is best used for this activity.

+3
source share
3 answers

1) ... but the problem is that it is apparently case sensitive, how can I change this?

. , , :

if (listToRedo[i].value.toLowerCase().indexOf(text.toLowerCase()) != -1) {

(i):

var reg = new RegExp(text, "i");
if (listToRedo[i].value.search(reg) != -1) {

, , , , . .

2) onkeyevent, onkeypress, onkeydown ? , .

onkeydown onkeyup . onkeypress , , Chrome IE, :

  • : A - Z ( )
  • : 0 - 9
  • :! @# $% ^ *() _ - + = < [] {},./?\| '`" ~
  • : ESC, ,

onkeypress onkeydown , .

+2
if(listToRedo[i].value.toLowerCase().indexOf(text.toLowerCase()) != -1){

, .

+2
  • Use two lists and convert all elements of one of them to lowercase. You can then hide the text in the box and lowercase before you search.

  • onkeydownand onkeyupis called for each key, even the cursor keys, shift, etc.

+2
source

Source: https://habr.com/ru/post/1760070/


All Articles