检查字符串中的第一个字符 [英] Check First Char In String

查看:33
本文介绍了检查字符串中的第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有输入框.我正在寻找一种方法来启动 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/

这篇关于检查字符串中的第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆