开发者

jquery plugin for character count

as per code provide by Felix, i have made a html page for the character count script.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" language="javascript" src="jquery.js"></script>
    <script type="text/javascript">
$(document).ready(function() {
    (function($) {
        $.fn.charlimit = function(options) {
            var def = {
            limit: 250,
                display: null
            };
            $.extend(def, options);
            var $display = $(def.display);

            $display.html(def.limit);

            this.bind('change keyup', function() {
                var l = $(this).val().length;
                $display.html(def.limit - l);
                if (l > def.limit) {
                    $(this).val(function(i, value) {
                        return value.s开发者_如何学运维ubstring(0, def.limit);
                    });
                    $display.html(def.limit - $(this).val().length);
                }
            });
            return this;
        };
    }(jQuery));


        //Plugin Call
        $('#message').charlimit({
            limit: 10,
            display: '#charCount'
        });
    });
</script>
</head>

<body>
    <textarea name="message" id="message" tabindex="4" rows="4" cols="35"></textarea>
    <p class="character_limit">Character limit: <span id="charCount"></span></p>
</body>
</html>

The problem i am facing is... when the character count becomes 0 and i press another key.. charcount will show -60 and the function(i, value) { ..... } gets inside the textarea.

Please check.

Thanks LOkesh Yadav


There seems to be a small syntax inaccuracy in there with the function declaration.. weird that this works, but maybe it's normal...

The last line of the function declaration is:

        };
    }(jQuery));

But should be:

        };
    })(jQuery);

edit: I didn't get an error in Firefox though, and it seemed to work fine, so this is probably not the culprit.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜