Input Radios Control

-1

Hello, I have the following code below where I wanted to control the radios. I want to have an "event" triggered when the each one ends scanning the entire group of radios 1 whose name is "group1". That is, at the end of each group of radios swept a message or a variable is changed its status. ex: When arriving at group1 with value 4 a message is sent. When arriving at the last element of group2 must also be triggered "event". This has to be DYNAMIC. For any other or amount of radios elements.

$(function() {

  $("#botao").click(function() {

    $("textarea[name='arraytextArea[]'],input[name='grupo1'],input[name='grupo2']").each(function(i, el) {
      alert("Valor:" + $(el).val());
    });
  })
});
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script></head><body><div><textareaname='arraytextArea[]'></textarea><br><inputtype="radio" name="grupo1" value="Valor1"><span>Valor1</span>

    <input type="radio" name="grupo1" value="Valor2"><span>Valor2</span>

    <input type="radio" name="grupo1" value="Valor3"><span>Valor3</span>

    <input type="radio" name="grupo1" value="Valor4"><span>Valor4</span>

    <br><textarea name='arraytextArea[]'></textarea> <br>

    <input type="radio" name="grupo2" value="Valor1"><span>Valor2</span>

    <input type="radio" name="grupo2" value="Valor2"><span>Valor3</span>

    <input type="radio" name="grupo2" value="Valor3"><span>Valor4</span>

    <input type="radio" name="grupo2" value="Valor4"><span>Valor5</span><br>

    <input type="button" value="Enviar" id="botao">

  </div>
</body>

</html>
    
asked by anonymous 28.02.2018 / 20:01

1 answer

0

From what I understand you want a function to "group" all inputs by name and count them

Use the following code

 $(function () {
    $("#botao").click(function () {
        var inputs =  $(":radio");
        var grupos = [];

        var fe = $.each(inputs,function(){
        var nome = $(this).attr("name");
            if(typeof grupos[nome] === 'undefined'){
                grupos[nome] = 0;
           }
           grupos[nome] = grupos[nome] + 1;
        })
        setTimeout(function(){
            console.log(grupos);
        },1000)
    })
});

The output will be equal to [group1: 4, group2: 4] You can see a example working here

You can get the values as follows groups ['group1']; groups ['group2']; or any other name you are using

    
28.02.2018 / 20:52