简体   繁体   中英

JavaScript input contains item in array

I was wondering if anybody know how to use the code below to make it so that if the input contains an item of the javascript array it will redirect the page using window.location.href? Here is the code I have currently:

      var x = ["1", "2", "3"];


                  function validateForm() {
                    var x = document.forms["myForm"]["value"].value;
                    if (x != "59864") {
                      alert("doesnt contain");
                      return false;
                    }


                    var x = document.forms["myForm"]["value"].value;
                    if (x == "59864") {
                      window.location.href = "index.php";

                      return false;
                    }


                  }

Here is an example of how it can be done. I have added a keyup listener, but you may want to use a different event (change, keydown, paste, etc)

 document.getElementById("I").addEventListener("keyup", function() { console.log("here: " +this.value); if (this.value == "12345") { console.log("go to url"); } });
 <input id="I" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM