检查字符串中的第一个字符

2022-01-12 00:00:00 string char jquery javascript

我有输入框.我正在寻找一种方法来启动 alert() 如果给定字符串的第一个字符等于 '/'...

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 = $( '#screameria input' ).val();

if ( scream.charAt( 0 ) == '/' ) {

  alert( 'Boom!' );

}

目前这是我的代码.它不起作用,我认为这是因为该浏览器不知道何时检查该字符串...每当用户输入/"作为第一个字符时,我都需要该警报.

It's my code at the moment. It doesn't work and I think that it's because that browser doesn't know when to check that string... I need that alert whenever user inputs '/' as first character.

推荐答案

试试这个:

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

      alert( 'Boom!' );

    }
})

小提琴:http://jsfiddle.net/maniator/FewgY/

相关文章