Help in the focus event, does not want to work

1

Textbox focus event does not work, x value is correct, but component does not change its value, any hint, please.

    $(document).ready(function () {
        $('#txtSchool').textbox('textbox').bind('keypress focus blur', function (e) {
            CallEvents($(this), e);})
    });

    function CallEvents(Sender, e) {            
        if (e.type == 'focus') {
            var rd = $(Sender).prop('readonly');
            var rd = false; //testing...
            if (!rd) {
                var q = String($(Sender).val());
                var x = q.replace(/\./g, "a");   //replacing dot by a
                //$.messager.alert('SCObraNet', 'focus ' + x, 'info');
                $(Sender).val(x);   //não preenche o campo solicitado com o conteúdo x
                return;
            }
        }
        else if (e.type == 'keypress') {
            //$.messager.alert('SCObraNet', 'keypress ', 'info');
        }
        else if (e.type == 'blur') {
            //$.messager.alert('SCObraNet', 'blur', 'info');
        }
    };
</script>

I tested it in other ways and nothing, for example

    $('#txt').textbox('textbox').on('focus', function () {
        $(this).val("00000");         //don't set
    });

    or

    $('#txt').textbox('textbox').bind('focus', function () {
        $(this).val("00000");         //don't set
    });

    or

    $('#txt').textbox('textbox').bind('focusin', function () {
        $(this).val("00000");         //don't set
    });
    
asked by anonymous 08.02.2016 / 08:04

1 answer

1

The function CallEvents accepts two arguments. The this and the event . You are passing "garbage" to the function as ; and ) more. You do not need to pass $(this) because CallEvents already does $(Sender) , so just:

CallEvents(this, e);

You could also change this CallEvents function to:

$(document).ready(function () {
    $('#txtSchool').textbox('textbox').bind('keypress focus blur', CallEvents);
});

function CallEvents(e) { 
    var Sender = $(this);           
    if (e.type == 'focus') {
        var rd = Sender.prop('readonly');
        var rd = false; //testing...
        if (!rd) {
            var q = String(Sender.val());
            var x = q.replace(/\./g, "a");   //replacing dot by a
            //$.messager.alert('SCObraNet', 'focus ' + x, 'info');
            Sender.val(x);   //não preenche o campo solicitado com o conteúdo x
            return;
        }
    }
    else if (e.type == 'keypress') {
        //$.messager.alert('SCObraNet', 'keypress ', 'info');
    }
    else if (e.type == 'blur') {
        //$.messager.alert('SCObraNet', 'blur', 'info');
    }
};
    
08.02.2016 / 09:25