检查第一个字符串字符串 [英] Check First Char In String
本文介绍了检查第一个字符串字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有输入框。我正在寻找一种方法来启动 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屋!
查看全文