Check First Char In String

后端 未结 2 545
离开以前
离开以前 2020-12-17 17:28

I have input-box. I\'m looking for a way to fire-up alert() if first character of given string is equal to \'/\'...

var scream = $( \'#screameri         


        
相关标签:
2条回答
  • 2020-12-17 17:52

    Try this out:

    $( '#screameria input' ).keyup(function(){ //when a user types in input box
        var scream = this.value;
        if ( scream.charAt( 0 ) == '/' ) {
    
          alert( 'Boom!' );
    
        }
    })
    

    Fiddle: http://jsfiddle.net/maniator/FewgY/

    0 讨论(0)
  • 2020-12-17 18:07

    You need to add a keypress (or similar) handler to tell the browser to run your function whenever a key is pressed on that input field:

    var input = $('#screameria input');
    input.keypress(function() {
      var val = this.value;
      if (val && val.charAt(0) == '/') {
        alert('Boom!');
      }
    });
    
    0 讨论(0)
提交回复
热议问题