Audit of JS text (Jquery)



  • How do you check the text for predetermined key words or expressions?

    There's something. <textarea> There must be a text in it originally, then the user turns in and if, what he's got in common with those keys that are predetermined to show him the button.

    In general, there were two small violators, but the first was only working if there was one key word and the second was working as it was, but only if the text had been introduced in advance and it did not respond to the new text.

    First violin

    <script type="text/javascript"> 
    function check (id, sbm) { 
    if (document.getElementById(id).value == 'привет') document.getElementById(sbm).style.display = ''; 
    };
    </script> 
    <form> 
    <textarea type="text" id="pole"></textarea> 
    <input type="button" value="Проверить" onclick="check('pole','sbm');"> 
    <input type="submit" id="sbm" value="Далее" style="display:none"> 
    </form>
    

    Second violin

     <script> 
        $(document).ready(function (){ 
        $(".check").click(function check(){ 
        $("textarea:contains(привет)").html(function(){$("#next").css("display", "block");}) 
          }); 
        }); 
       </script>
    

    I'll be very grateful for any help, because the JS won't be strong for two days.



  • It's really simple. Referring to documentation jquery Make the following conclusion or slight modification:

    $( "form" ).submit(function( event ) {
      var check = $( "input:first" ).val()
      if ( check == "correct" || check == "ok" || check == "good") {
        $( "span" ).text( "Validated..." ).show();
        return;
      }
    

    $( "span" ).text( "Not valid!" ).show().fadeOut( 1000 );
    event.preventDefault();
    });

    or more encumbered option:

    $( "form" ).submit(function( event ) {
    var check = $( "input:first" ).val()
    if ( ["correct", "ok", "good"].indexOf($( "input:first" ).val().toLowerCase())!= -1) {
    $( "span" ).text( "Validated..." ).show();
    return;
    }
    $( "span" ).text( "Not valid!" ).show().fadeOut( 1000 );
    event.preventDefault();
    });

    In this case, we propose a range of possible values plus extra bonus: .toLowerCase Make sure that OK == ok

    It's an example. http://codepen.io/NeedHate/pen/Ywzaav ♪




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2