How to dynamically pass input name to Jquery

0

I have the following function in jquery in my script PHP

$('.myinput').on('blur', function(e) {
    $.ajax({
        url: 'gravadado.php',
        method: 'POST', // ou POST
        data: {
            q10600: this.value
        },
        success: function(resposta) {
            var div = document.querySelector('#divDestino');
            div.innerHTML = resposta;
        }
    });
});

I have several inputs with the same class (.myinput) but in my jquery where I pass the name statically I want to pass the name of the input that called the event.

That is, if it was the input with name q10630 that triggered the event Onblur of Jquery I want to pass on my function Jquery the name q10630 but if it is the name of the input q10700 that triggered the event I'm going to pass this name 'q10700'. I do not know if it was very confusing. And I do not know if this is possible either way.

Or if I have to give a class name for each input I have and create an event for each of these Onblur classes separately?!? Because I actually have many inputs in my code HTML . Then you would get hundreds of onblur events if you have to reference each Input with a different class name. Do you understand?

    
asked by anonymous 21.08.2017 / 22:24

1 answer

1

Create an object and pass keys to the value you want in this case the input name , and in ajax you pass the entire object ;

$('.myinput').on('blur', function(e) {

    var name = $(this).attr('name');
    var data = {};
    data[name] = $(this).attr('value');

    $.ajax({
        url: 'gravadado.php',
        method: 'POST', // ou POST
        data: data,
        success: function(resposta) {
            var div = document.querySelector('#divDestino');
            div.innerHTML = resposta;
        }
    });
});
    
21.08.2017 / 22:32