合并冗余的.each/.keyup jQuery脚本

合并冗余的.each/.keyup jQuery脚本,jquery,merge,Jquery,Merge,我继承的这个wordcount脚本工作得很好,但将同一个脚本编写两次以处理页面加载和键控时的wordcount似乎是多余的 如果只写一次,并且在两个地方都使用的话,看起来会更干净、更高效。我相信这很简单,但我就是不知道我该如何设置它 $('.wordcount').each(function() { var plural_characters = '', plural_words = '', character_length = $(this).val()

我继承的这个wordcount脚本工作得很好,但将同一个脚本编写两次以处理页面加载和键控时的wordcount似乎是多余的

如果只写一次,并且在两个地方都使用的话,看起来会更干净、更高效。我相信这很简单,但我就是不知道我该如何设置它

$('.wordcount').each(function() {
    var plural_characters = '',
        plural_words = '',
        character_length = $(this).val().length,
        word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
        counter_span = $(this).parents('tr').find('.counter');

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if ($(this).val() == '') {
        plural_words = 's';
        word_length = '0';
    }

    counter_span
        .html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);

    $(this).keyup(function() {
        var plural_characters = '',
            plural_words = '',
            character_length = $(this).val().length,
            word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
            counter_span = $(this).parents('tr').find('.counter');

        if (character_length != 1) {
            plural_characters = 's';
        }

        if (word_length != 1) {
            plural_words = 's';
        }

        if ($(this).val() == '') {
            plural_words = 's';
            word_length = '0';
        }

        counter_span
            .html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);
    });
});
$('.wordcount')。每个(函数(){
变量复数字符=“”,
复数单词=“”,
字符长度=$(this).val().length,
单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length,
counter_span=$(this).parents('tr').find('.counter');
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
if($(this.val()=''){
复数单词='s';
单词长度='0';
}
柜台
.html(字符长度+“字符”+复数字符+“
”+单词长度+“单词”+复数单词); $(this).keyup(function(){ 变量复数字符=“”, 复数单词=“”, 字符长度=$(this).val().length, 单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length, counter_span=$(this).parents('tr').find('.counter'); 如果(字符长度!=1){ 复数字符='s'; } 如果(字长!=1){ 复数单词='s'; } if($(this.val()=''){ 复数单词='s'; 单词长度='0'; } 柜台 .html(字符长度+“字符”+复数字符+“
”+单词长度+“单词”+复数单词); }); });
我用这个脚本设置了一个基本的小提琴:


任何帮助都将不胜感激

绑定
keyup
处理程序,然后触发它:

来自fiddle的完整代码:

$('.wordcount').keyup(function () {
    var plural_characters = '',
        plural_words = '',
        character_length = $(this).val().length,
        word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
        counter_span = $(this).parents('tr').find('.counter');

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if ($(this).val() == '') {
        plural_words = 's';
        word_length = '0';
    }

    counter_span.html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);
}).trigger('keyup');
$('.wordcount').keyup(函数(){
变量复数字符=“”,
复数单词=“”,
字符长度=$(this).val().length,
单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length,
counter_span=$(this).parents('tr').find('.counter');
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
if($(this.val()=''){
复数单词='s';
单词长度='0';
}
计数器span.html(字符长度+字符+复数字符+'
'+单词长度+单词+复数单词); }).触发器(“键控”);
绑定
keyup
处理程序,然后触发它:

来自fiddle的完整代码:

$('.wordcount').keyup(function () {
    var plural_characters = '',
        plural_words = '',
        character_length = $(this).val().length,
        word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
        counter_span = $(this).parents('tr').find('.counter');

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if ($(this).val() == '') {
        plural_words = 's';
        word_length = '0';
    }

    counter_span.html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);
}).trigger('keyup');
$('.wordcount').keyup(函数(){
变量复数字符=“”,
复数单词=“”,
字符长度=$(this).val().length,
单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length,
counter_span=$(this).parents('tr').find('.counter');
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
if($(this.val()=''){
复数单词='s';
单词长度='0';
}
计数器span.html(字符长度+字符+复数字符+'
'+单词长度+单词+复数单词); }).触发器(“键控”);
绑定
keyup
处理程序,然后触发它:

来自fiddle的完整代码:

$('.wordcount').keyup(function () {
    var plural_characters = '',
        plural_words = '',
        character_length = $(this).val().length,
        word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
        counter_span = $(this).parents('tr').find('.counter');

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if ($(this).val() == '') {
        plural_words = 's';
        word_length = '0';
    }

    counter_span.html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);
}).trigger('keyup');
$('.wordcount').keyup(函数(){
变量复数字符=“”,
复数单词=“”,
字符长度=$(this).val().length,
单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length,
counter_span=$(this).parents('tr').find('.counter');
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
if($(this.val()=''){
复数单词='s';
单词长度='0';
}
计数器span.html(字符长度+字符+复数字符+'
'+单词长度+单词+复数单词); }).触发器(“键控”);
绑定
keyup
处理程序,然后触发它:

来自fiddle的完整代码:

$('.wordcount').keyup(function () {
    var plural_characters = '',
        plural_words = '',
        character_length = $(this).val().length,
        word_length = $(this).val().split(/\b[\s,\.-:;]*/).length,
        counter_span = $(this).parents('tr').find('.counter');

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if ($(this).val() == '') {
        plural_words = 's';
        word_length = '0';
    }

    counter_span.html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);
}).trigger('keyup');
$('.wordcount').keyup(函数(){
变量复数字符=“”,
复数单词=“”,
字符长度=$(this).val().length,
单词长度=$(this.val().split(/\b[\s,\.-:;]*/).length,
counter_span=$(this).parents('tr').find('.counter');
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
if($(this.val()=''){
复数单词='s';
单词长度='0';
}
计数器span.html(字符长度+字符+复数字符+'
'+单词长度+单词+复数单词); }).触发器(“键控”);
以下是我如何清理这些代码:

function wordCount(object) {
    var plural_characters = '';
    var plural_words = '';
    var character_length = object.val().length;
    var word_length = object.val().split(/\b[\s,\.-:;]*/).length;

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if (object.val() == '') {
        plural_words = 's';
        word_length = '0';
    }
    object.parent().siblings('.label').children('.counter').html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);

}

wordCount($('.wordcount:first'));

$(document).ready(function(){
    $('.wordcount').keyup(function(){
        wordCount($(this));
    });
});
函数字数(对象){
变量复数字符=“”;
var复数单词=“”;
var character_length=object.val().length;
var word_length=object.val().split(/\b[\s,\.-:;]*/).length;
如果(字符长度!=1){
复数字符='s';
}
如果(字长!=1){
复数单词='s';
}
如果(object.val()=''){
复数单词='s';
单词长度='0';
}
object.parent().sides('.label').children('.counter').html(字符长度+'字符'+复数字符+'
'+单词长度+'单词'+复数单词); } 字数($('.wordCount:first'); $(文档).ready(函数(){ $('.wordcount').keyup(函数(){ 字数($(this)); }); });
下面是我的JSFIDLE,还有一些评论:

以下是我如何清理此代码:

function wordCount(object) {
    var plural_characters = '';
    var plural_words = '';
    var character_length = object.val().length;
    var word_length = object.val().split(/\b[\s,\.-:;]*/).length;

    if (character_length != 1) {
        plural_characters = 's';
    }

    if (word_length != 1) {
        plural_words = 's';
    }

    if (object.val() == '') {
        plural_words = 's';
        word_length = '0';
    }
    object.parent().siblings('.label').children('.counter').html(character_length + ' character' + plural_characters + '<br />' + word_length + ' word' + plural_words);

}

wordCount($('.wordcount:first'));

$(document).ready(function(){
    $('.wordcount').keyup(function(){
        wordCount($(this));
    });
});
函数字数(对象){
变量复数字符=“”;
var复数单词=“”;
var character_length=object.val().length;
var word_length=object.val().split(/\b[\s,\.-:;]*/).length;
如果