dongwubao4785 2014-07-24 04:19
浏览 194
已采纳

针对退格的textarea的Keypress事件

In my project, I am showing the remaining character length for a textarea for adding reviews, in a span while user types on the textarea.

The following is the keypress event :

$('#reviewtxt').keypress(function(){

            if($('#reviewtxt').val().length > 1000){
                return false;
            }
            if($.trim($('#reviewtxt').val()) != "")
            {
                $("#reviewremaining").html("Remaining Characters : " +(1000 - $('#reviewtxt').val().length));
            }
        });

HTML Code for the textarea

$data = array(
        'name'        => 'reviewtxt',
        'id'          => 'reviewtxt',
        'value'       => '',
        'rows'        => '10',
        'cols'        => '50',
        'style'       => 'border-radius:3px;border:1px solid rgba(75,156,255,1);margin-top:10px;',
        'placeholder'   => lang('type_here').' (Press Enter key to send your review) ',
        'onkeypress'    => 'submitreview(\''.base_url().'\',\''.$pgmresult->id.'\',\''.$id.'\',event,\'0\')'
        );
        echo form_textarea($data);?>
        <span id='reviewremaining' style="color:red;font-size: 13px;"></span> 

AJAX :

function submitreview(baseurl,videoid,userid,e,type)
{ 
    $("#reviewremaining").show();
    $("#reviewremaining").html('');
    var reviewtxt=$.trim($("#reviewtxt").val());
    if (e.keyCode == 13 || e.which == 13)
    {
        if(reviewtxt=="")
        { 
            $("#reviewremaining").html('Please enter your review');
            $("#reviewtxt").focus();
            return false;
        }
        else
        { 
            if(reviewtxt.length>1000)
            {
                $("#reviewremaining").html('Review cannot exceed more than 1000 characters');
                $("#reviewtxt").focus();
                return false;
            }
            $.ajax({
                type:"post",
                dataType:"text",
           data:"reviewtxt="+reviewtxt+"&videoid="+videoid+"&userid="+userid+"&type="+type,
                url:baseurl+"video/submitreview",
                success:function(response)
                { 

                }
            });
        }
    }
}

Now the problem is that, I am able to press another key even if the length exceeds the maximum. Suppose the maximum length is 500 characters then, the function will return false only when I press 501th character so that I need to delete that character in order to save the review. Also I want to show the remaining characters when I click on backspace or delete button.

Currently if I type 300 characters, then it will show Remaining characters : 300, then if I copy-paste some other lines, the text in span will still remain as same.

How to fix this problem? Can anyone help e to fix this?

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • douzhun5971 2014-07-24 04:35
    关注

    use keyup event and call same function mouseup too

    Here is fiddle

    $('#reviewtxt').keyup(function()
    

    Updated Fiddle

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛