0

次のjqueryコードを使用して、テキストエリアに入力する文字数が残っていることを示しました。これは、すべてのブラウザーで正常に機能します。
しかし、バックスペースを使用して一部の文字を削除すると、Mozilla では何文字残っているかが表示されますが、IE、Chrome、safari では機能しません。

これはデモリンクです: http://jsfiddle.net/ckWNM/

jQuery(document).ready(function($) {
    var max = 10;
    $('textarea.max').keypress(function(e) {
        $("#counter").html("characters left : " +(10 - this.value.length));
        if (e.which < 0x20) {
            return;   
        }
        if (this.value.length == max) {
            e.preventDefault();
        } else if (this.value.length > max) {
            // Maximum exceeded
            this.value = this.value.substring(0, max);
        }
    });
}); //end if ready(fn)
4

1 に答える 1

0

残っている文字も更新するkeyupと、Chrome でこれを修正できます。

jsフィドル

jQuery(document).ready(function($) {
    var max = 10;
    $('textarea.max')
        .keypress(refreshAmount)
        .keyup(refreshAmount);
}); //end if ready(fn)

function refreshAmount(e) {
    $("#counter").html("characters left : " +(10 - this.value.length));
    if (e.which < 0x20) {
        return;   
    }
    if (this.value.length == max) {
        e.preventDefault();
    } else if (this.value.length > max) {
        // Maximum exceeded
        this.value = this.value.substring(0, max);
    }
}
于 2013-03-28T11:16:02.847 に答える