Pergunta

I have a script to count the number of fields that are filled. Works great. But I discovered the script only counts the fields that DO NOT have input hints. Is there any work around?

http://jsbin.com/oguzaj/4

SCRIPT TO COUNT FIELDS:

$(document).ready(function () {
    (function ($) {

        $('#form_register').on('keyup change', function () {
            var number = 0;
            $(this).find('input, textarea').each(function () {
                if (this.value !== '') {
                    $('.input_count').val(number++);
                }
            });
        });

    })(jQuery);

});

SCRIPT FOR INPUT HINTS:

(function ($) {
    $.fn.hint = function (blurClass) {
        if (!blurClass) {
            blurClass = 'blur';
        }
        return this.each(function () {
            var $input = $(this),
                title = $input.attr('title'),
                $form = $(this.form),
                $win = $(window);

            function remove() {
                if ($input.val() === title && $input.hasClass(blurClass)) {
                    $input.val('').removeClass(blurClass);
                }
            }
            if (title) {
                $input.blur(function () {
                    if (this.value === '') {
                        $input.val(title).addClass(blurClass);
                    }
                }).focus(remove).blur();
                $form.submit(remove);
                $win.unload(remove);
            }
        });
    };
})(jQuery);

$(function () {
    $('input[title!=""]').hint();
    $('textarea[title!=""]').hint();
});
Foi útil?

Solução

$(document).ready(function() {
        $('#form_register').on('keyup change', function() {
            var number = 0;
            $(this).find('input, textarea').each(function() {
                if ($(this).val() && !$(this).hasClass('blur')) {
                    $('.input_count').val(number++);
                }
            });
        });

});

Edit: If you've got an button which fills one field you'll need to add this to the button.

$(buttonSelector).on('click', function() {
    var number = 0;
    $('#form_register').find('input, textarea').each(function() {
        if ($(this).val() && !$(this).hasClass('blur')) {
            $('.input_count').val(number++);
        }
    });
});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top