2013-04-10 69 views
0

下面是我已经写和它的正常工作:字符类型检测

var $remaining = $('#remaining'), 
$messages = $remaining.next(); 

$('#message').keyup(function(){ 
var chars = this.value.length, 
    messages = Math.ceil(chars/160), 
    remaining = messages * 160 - (chars % (messages * 160) || messages * 160); 

$remaining.text(remaining + ' characters remaining'); 
$messages.text(messages + ' message(s)'); 

}); This is the above code in action

现在我想要做的是:

var text_max_En = 100; 
var text_max_utf8 = 200; 
if /* what user has typed is English */ 
{ 
    Do this 
} 
else if /* what user has typed is in utf8 type*/ 
{ 
    Do that 
} 

简而言之:

**If**用户在输入英文有那么它应该数到每个消息

100个字符

AND **if**这是在urf8类型,那么就应该数到200人。

任何解决方案?

+0

尝试编写干,使您的问题更加明显。 – Andries 2013-04-10 16:52:40

+0

不好意思,干吗? – John 2013-04-10 16:57:49

+1

'utf8 type'是什么?你能给个例子吗? – 2013-04-10 17:02:29

回答

1

使用String.charCodeAt得到的任何数值或字符串中的所有字符。如果值大于127,那么你正在处理unicode值。

既然你是回调keyUp,你可以做到这一次一个:

var str = $('#textarea').val(); 
var ascii = str.charCodeAt(str.length - 1) < 128; 

(您也可以直接通过字符串长度循环,但这是多余的,因为你表现一个按键在一个时间。)

+0

我会尽力,尽管实施它对我来说并不容易。谢谢 – John 2013-04-10 17:41:54

+0

我完全搞砸了。我应用了你的建议,但不工作。我认为有一些语法问题,而不是逻辑问题。 [检查这里](http://jsfiddle.net/xqyWV/) – John 2013-04-10 18:05:34

+0

你是指在我的问题或[这一个](http://jsfiddle.net/kTukB/)的小提琴? – John 2013-04-10 18:11:02