0

当用户输入 input 时,我的 textarea 可以计算字符串/字符。这是我的jsfiddle

我的问题是我想设置条件意味着当字符串/字符高于 158 时,我想在里面显示消息<div id="message">We will deduct 2 credit from your account</div>,当字符串/字符高于 316 时,消息将更新并显示<div id="message">We will deduct 3 credit from your account</div>并继续..

例子 :

158 *2

158 = We will deduct 2 credit from your account
316 = We will deduct 3 credit from your account
474 = We will deduct 4 credit from your account
632 = We will deduct 5 credit from your account
...
4

5 回答 5

2

尝试一下

$('#myInput').keyup(function() {
    $('#charCount').text(this.value.length);

    var c = parseInt(this.value.length / 158);
    if(c > 0) 
        $('#message').text('We will deduct '+c+' credit from your account');
});​
于 2012-10-30T07:59:30.763 回答
0

1) 查找字符串的长度

2)除以158

3)在答案中添加“1”

4)使用字符串连接创建消息-“我们将扣除”+(3)的输出+“您的帐户中的信用”

5) jQuery("#message").html("我们将扣除" + (3) 的输出 + "您的账户贷记");

于 2012-10-30T07:58:51.850 回答
0

试试这个

$('#myInput').keyup(function() {
    var len = this.value.length ; 
    $('#charCount').text(len);
    var creditCount = 158;
    if( len > creditCount ){
         var credits = Math.floor(len / creditCount );
         $('#message').html('We will deduct '+  credits  + ' credit from your account;' )
    }
});​

检查演示

于 2012-10-30T08:00:56.260 回答
0

在 textarea keyup 上,您可以检查否。字符数,因此您可以编写 if-else 条件来显示消息。

检查这个:工作小提琴

$('#myInput').keyup(function() {
    if(this.value.length<=158)
    $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 || this.value.length<316)
    $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316|| this.value.length<316)
    $('#charCount').text('We will deduct 3 credit from your account');
});​
于 2012-10-30T08:09:06.303 回答
0
$('#myInput').keyup(function() {
    $('#charCount').text(this.value.length);
    var length = this.value.length;
    if (length % 158 == 0) {
      $('#message').html('We will deduct '+ (length/158) + 
                         ' credit from your account;' )                        
    }
});
于 2012-10-30T08:13:06.823 回答