Javascript поисковый ввод - PullRequest
0 голосов
/ 21 апреля 2020

У меня есть следующий код.

 data.forEach((element) => {
//nuova feat di ES6 , passando i dati in questo modo le mie const assumono il valore di element.confirmed , etc
console.log(element);
var {
  confirmed,
  countryregion,
  location,
  provincestate,
  deaths,
  recovered,
} = element;
// console.log(countryregion);
window.onkeyup = keyup;

//creates a global Javascript variable
var inputTextValue;

function keyup(e) {
  //setting your input text to the global Javascript Variable for every key press
  inputTextValue = e.target.value;

  //listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
  if (e.keyCode == 13) {
    if (inputTextValue == countryregion || inputTextValue == provincestate) {
      return console.log("THIS OBJECT");
    } else {
      return console.log("THIS OBJECT DON'T EXIST");
    }
  }
}

Я хотел бы сделать поисковый ввод. На практике я хотел бы убедиться, что когда элемент, который пользователь вставляет в поисковый ввод, равен провинции или стране, он возвращает все данные, относящиеся к этому объекту. Например, если я напишу Марокко на входе, я верну этот объект здесь со смертельными исходами, восстановленными и подтвержденными значениями. Например, enter image description here Данные массива - это массив объектов из API. Но на самом деле у меня возникают различные проблемы, сравнение проводится только с последним элементом провинции или страны. Вы можете мне помочь?

Ответы [ 3 ]

0 голосов
/ 21 апреля 2020

    let countryregion = "abc";
    let provincestate = "def";
    window.document.getElementById("inputHere").onkeyup = keyup;

        //creates a global Javascript variable
        var inputTextValue;

        function keyup(e) {
            //setting your input text to the global Javascript Variable for every key press
            inputTextValue = e.target.value;

            //listens for you to press the ENTER key, at which point your web address will change to the one you have input in the search box
            if (e.keyCode == 13) {
                if (inputTextValue == countryregion || inputTextValue == provincestate) {
                    return console.log("THIS OBJECT " + inputTextValue);
                } else {
                    return console.log("THIS OBJECT DON'T EXIST");
                }
            }else{
            
            }
        }
<input id="inputHere"/>
0 голосов
/ 21 апреля 2020

, если вы не хотите использовать тег ввода

    let countryregion = "abc";
    let provincestate = "def";
    let inputTextValue = "";
    window.onkeyup = keyup;
    document.getElementById("clear").onclick = function(){
            inputTextValue = "";
            document.getElementById("search").innerText = inputTextValue;;
    }


        function keyup(e) {
            //setting your input text to the global Javascript Variable for every key press

            //listens for you to press the ENTER key, at which point your web address will change to the one             you have input in the search box
            if (e.keyCode == 13) {
                if (inputTextValue.toLowerCase() == countryregion.toLowerCase() ||     inputTextValue.toLowerCase() == provincestate.toLowerCase()) {
                    return console.log("THIS OBJECT " + inputTextValue);
                } else {
                    return console.log("THIS OBJECT DON'T EXIST " + inputTextValue);
                }
            }else{
                inputTextValue += String.fromCharCode(e.keyCode);
                document.getElementById("search").innerText = inputTextValue;;
            }
        }
<div id="search">type text then press Enter</div>
<button id="clear">clear</button>
0 голосов
/ 21 апреля 2020

Измените условие if, как показано ниже, надеюсь, это поможет

if (e.keyCode == 13) {
   if (element['countryregion'] == inputTextValue || element['provincestate'] == inputTextValue) {
          return console.log("THIS OBJECT");
        } else {
          return console.log("THIS OBJECT DON'T EXIST");
        }
      }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...