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 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”