Control of Radios Input

Asked

Viewed 32 times

-1

Hello, I have the following code below where I wanted to control the radios. I want there to be an "event" triggered when each finishes sweeping the entire group of radios 1 whose name is "group1". That is, at the end of each group of scanned radios a message is sent or a variable changes its status. ex: when you arrive in group 1 with value 4, a message must be sent. When you arrive at the last element of Grupo2, it must also trigger "event". This has to be DYNAMIC.For any other or quantity of radio 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>
    <textarea name='arraytextArea[]'></textarea> <br>

    <input type="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>

  • Wouldn’t it just be putting an Alert after the end of the foreach? I don’t know if I understood you correctly.

  • I realized another question ... This question was misworded.I told you what I really want to do at this link :https://answall.com/questions/279667/concatenar-elements-radio

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 exit will be equal to [Group 1: 4, Group 2: 4] You can see a example working here

You can get the values as follows groups['group1']; groups['Group 2']; or any other name you’re using

  • Note that I am logging the value in the console so press F12 to see the result

  • It is really the question was poorly formulated. Actually I wanted to know when each was in the last element of each group because, my intention was to concatenate each span element according to the group that it is part of

Browser other questions tagged

You are not signed in. Login or sign up in order to post.