如何创建一个函数并在事件上调用它

时间:2009-12-20 21:55:34

标签: jquery function textbox character counter

我已经包含了我放在一起的textbox / textarea计数器的代码。 (希望它不会在我的帖子中被破坏......)

该脚本运行良好,但是由于我对jquery和javascript缺乏经验,我知道我的代码非常臃肿。

我希望有人可以告诉我如何获取重复代码(//事件一到五)并将其放入自己的函数中,然后在每个事件上调用该函数。

谢谢,

杰夫

$(document).ready(function() {


if ($('span[class=tbc] input[type=text], span[class=tbc] textarea')) {        
    $('span[class=tbc] input[type=text], span[class=tbc] textarea').each(function() {

       var maxL = $(this).attr('MaxLength');

        $(this).siblings('span[class=tbc]').prepend('<br /><span id="spanID1">Characters remaining: </span>'); 
        $(this).siblings('span[class=tbc]').append('<strong>' + maxL + '</strong>');


            //event one
            $(this).keyup(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end keyup function  


            //event two
            $(this).keydown(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end keydown function  


            //event three
            $(this).mouseout(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end mouseout function 


            //event four
            $(this).mousemove(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end mousemove function 


            //event five
            $(this).blur(function() {
            var cnt = $(this).val().length;
            var chars = maxL-cnt;
            var mesg = $(this).parent('span').attr('ID');

            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

            if (cnt > maxL - 1) {   
            //alert('Cannot enter anymore characters.');
            $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
            }
            }); //end blur function 


    });  //end each loop
} //end if ($('span[class=tbc] input[type=text] 

}); //end ready

4 个答案:

答案 0 :(得分:0)

以下是您尝试实现的示例:

http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial

它展示了如何从你拥有的插件中获取。

答案 1 :(得分:0)

var handler = function() {
        var cnt = $(this).val().length;
        var chars = maxL-cnt;
        var mesg = $(this).parent('span').attr('ID');

        $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

        if (cnt > maxL - 1) {   
        //alert('Cannot enter anymore characters.');
        $(this).siblings('span[class=tbc]').html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
        }
};

$(this).keyup(handler);
$(this).keydown(handler);
// etc.

答案 2 :(得分:0)

您可以使用jQuery中的bind方法绑定事件列表,而不是逐个绑定它们:

$(document).ready(function() {

    // No need for if since .each() will do nothing on an empty jQuery    
    $('span[class=tbc] input[type=text], span[class=tbc] textarea').each(function() {

       var maxL = $(this).attr('MaxLength');

       // Try to save queries that you use a lot
       var $tbc = $(this).siblings('span[class=tbc]');

       // You can use append() and prepend() in the same jQuery chain
       $tbc.prepend('<br /><span id="spanID1">Characters remaining: </span>')
           .append('<strong>' + maxL + '</strong>');

            // Bind all events at once
            $(this).bind("keyup keydown mouseout mousemove blur", function() {
                var cnt = $(this).val().length;
                var chars = maxL-cnt;
                var mesg = $(this).parent('span').attr('ID');

                $tbc.html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');      

                if (cnt > maxL - 1) {   
                    //alert('Cannot enter anymore characters.');
                    $tbc.html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>'); 
                }
            }); //end bind


    });  //end each loop

}); //end ready

我稍稍调整了你的代码,这样你就可以学习一些jQuery技巧。

答案 3 :(得分:0)

$(document).ready(function() {
    $('span.tbc').find('input[type=text], span.tbc textarea').each(function() {
        var ele = $(this);
        var maxL = ele.attr('MaxLength');
        var siblings = ele.siblings('span.tbc');
        siblings
            .prepend('<br /><span id="spanID1">Characters remaining: </span>')
            .append('<strong>' + maxL + '</strong>');
        //event one-five
        ele.bind("keyup keydown mouseout blur mousemove", function() {
            var cnt = ele.val().length;
            var chars = maxL-cnt;
            var mesg = ele.parent('span').attr('ID');
            if (cnt > maxL - 1) {
                //alert('Cannot enter anymore characters.');
                siblings.html('<br /><span>Characters remaining: </span><span style="color: red;"><strong>' + chars + '</strong> (Cannot enter anymore characters.)</span>');
            } else {
                siblings.html('<br /><span>Characters remaining: </span><strong>' + chars + '</strong>');
            }
        }); //end event function
    });  //end each loop
});