简体   繁体   English

检查文本输入是否与if和else if语句中的多个数组之一内的一项匹配

[英]check if text input matches one item inside one of multiple arrays within if and else if statements in javascript

I have a page in a application where I want to have specific images pop up if a specific text is entered by the user. 我在应用程序中有一个页面,如果用户输入了特定的文本,我想弹出特定的图像。 I tried with one example and it worked, but now that I added several variables and their array rows, it does not work . 我尝试了一个示例,它确实起作用了,但是现在我添加了几个变量及其数组行,所以它不起作用 I can't tell exactly what mistake I have made here, I am guessing it's something to do with the nesting with the if and else if condition/statements 我无法确切地说出我在这里犯了什么错误,我想这可能与if和else if条件/语句的嵌套有关

HTML HTML

      <form>
        <input type="text" name="search" placeholder="Type or tap on the microphone above">
      </form>
      <div class="imageContainerSearchPage"> 


            <img id="toast_018" class="hiddenElement" src="animsAll/Anims_018_Toast.gif">
            <img id="nobaloons_014" class="hiddenElement" src="animsAll/Anims_014_no baloons.gif">
            <img id="wegotastory_019"  class="hiddenElement" src="animsAll/Anims_019_Button_We got a story_textandbook.gif">
            <img id="parisIsBurningButton_021" class="hiddenElement" src="animsAll/Anims_021_Paris is Burning Button.gif">
    </div>
    <!--[•VIDEO BACKGROUND•]-->
    <video  autoplay loop muted>
    <source src="videos/music_saint_etienne_only_love_cropped_370x660.mp4" type="video/mp4">
    Your browser does not support the video tag.
    </video></div>

JAVASCRIPT: not working JAVASCRIPT:不起作用

     $( "input" )
          .keyup(function() {
            var value = $( this ).val();
            var no = ["sixties", "sixty", "sixtys"];
            var weGotAStory = ["we", "got", "story"];
            var sandwich = ["sandwich", "grilled"];
            var parisIsBurningButton = ["paris", "burning"];
            var toast = ["toasted", "toast", "toasts"];
          if($.inArray(value, no) !== false) {
              $("#nobaloons_014").show().delay(10000).fadeOut();
          } else if ($.inArray(value, weGotAStory) !== false) {
            $("#wegotastory_019").show().delay(10000).fadeOut();
          } else if ($.inArray(value, parisIsBurningButton) !== false) {
            $("#parisIsBurningButton_021").show().delay(10000).fadeOut();
        } else ($.inArray(value, toast) !== false) {
          $("#toast_018").show().delay(10000).fadeOut();

        }

          })

JAVASCRIPT: working (single IF statement) JAVASCRIPT:正在运行(单个IF语句)

     $( "input" ).keyup(function() {
            var value = $( this ).val();
            var no = ["no", "nope", "nos"];
          if($.inArray(value, no) !== false) {
              $("#nobaloons_014").show().delay(10000).fadeOut();
         }

      })

I compromised with the arrays and took them out because nothing else was working. 我妥协了这些阵列,并把它们取出来,因为没有其他东西在起作用。 This works to make the hidden image corresponding to the string pop up but it's not exactly what I wanted (it does not offer the multiple word option w/o the arrays). 这可以使与字符串相对应的隐藏图像弹出,但是这并不是我想要的(它不提供不带数组的多字选项)。

$( "input" )
  .keyup(function() {
    var value = $( this ).val();
    var sixties = "sixties";
    var paris = "paris";
    var weGotAStory = "story";
  if (value == sixties) {
    $("#button1960s_002").show().delay(10000).fadeOut();
  }   else if (value == paris) {
      $("#parisIsBurningButton_021").show().delay(10000).fadeOut();
    } else if (value == weGotAStory) {
        $("#wegotastory_019").show().delay(10000).fadeOut();
      }

  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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