重新删除删除字符时剩余的字符

时间:2014-05-03 14:02:32

标签: jquery html textarea

我有这个脚本

http://jsfiddle.net/hungerpain/8gKs4/2/

我正在倒计时在文字区输入时留下的字符。但是当我删除字符时它不算数,我该怎么办呢?

HTML

<table>
        <tr>
            <td colspan="4" style="text-align:center;">NOTES
                <br/>
                <textarea class="sam_notes maxed" maxlength="750" name="sam_notes" style="height:100px;width:90%;margin:0 auto;"></textarea>
                <br/> <span style="font:normal 11px sans-serif;color:#B00400;">
             <span class='counter_msg'></span>
    </span>
            </td>
        </tr>
    </table>

JS

$('td').on('focus keypress', '.sam_notes', function (e) {

    var $this = $(this);
    var msgSpan = $this.parents('td').find('.counter_msg');
    var ml = parseInt($this.attr('maxlength'), 10);
    var length = this.value.length;
    var msg = ml - length + ' characters of ' + ml + ' characters left';

    msgSpan.html(msg);
});

2 个答案:

答案 0 :(得分:1)

Keypress仅在使用keydown的字符键上触发。

FIDDLE

答案 1 :(得分:0)

这不容易吗? purejs;

HTML

<textarea id="txt" onkeyup="count(255,'txt','msg');" maxlength="255"></textarea>

JS

function count(c,txtid,msgid){
            document.getElementById(txtid).innerText = c - document.getElementById(msgid).value.length;
            }

问候,Tymofek;