Sending a form just by clicking on the radio button

2

My question is this: Look at the following form fields:

input type="radio" name="pagto" value="A"
input type="radio" name="pagto" value="B"
input type="radio" name="pagto" value="C"
input type="radio" name="pagto" value="D"

Note that the "name" is the same and the variable is in "value", by default.

What I need: When you click on a radio, the form submission is already happening. I do not want to use the "submit" button, I want the submission to be at the moment of clicking the radio and that ALL fields of the form are sent.

    
asked by anonymous 13.06.2015 / 21:58

1 answer

3

You need to add a shadow of the change event. It would also give with click but change is more semantically correct and then make form.submit() when that event is detected.

An example would look like this:

var radios = document.querySelectorAll('input[type="radio"]');
[].forEach.call(radios, function (radio) {
    radio.addEventListener('change', function () {
        document.querySelector('form').submit();
    });
});

jsFiddle: link

If you already know what the form element can do as @ctgPi suggested , otherwise you can use this ( link ) that looks for the form element to which this input belongs.

// isto fica dentro do event handler
var form = (function (el) {
    while (el.tagName.toLowerCase() != 'form') el = el.parentNode;
    return el;
})(e.target);
form.submit();
    
13.06.2015 / 22:04