Check if at least one RADIO BUTTON has been checked

Asked

Viewed 39 times

2

I am creating a dynamic form, in it there can be X fields to fill, for example there can be 10 inputs Texts, 5 selects, 3 Radio etc

I created the whole structure to assemble this form in a dynamic way and it is working as expected.

But my biggest problem is when I will check if at least one RADIO BUTTON has been checked. If it were static the solution would be simpler, as it simply could take the ID of the main DIV and see if one is checked

<div id="$id">
 <?php for($i = $inicio; $i <= $fim; $i++){ ?>
   <label>
    <input  
     id="<?= $id ?>"    
     name="<?= $name ?>"
     value="<?= $i ?>" 
     type="radio"
   />
   <span><?= $i ?></span>
  </label>                                                 
<?php } ?>
</div>

They don’t need to pay too much attention to the details of the code, because I gave a summary, just to clarify that it is built dynamically

I needed to find some way to submit the form go through it all and check which are RADIOS from that individually go into each and check if at least one is checked, if you are not adding a message saying that the field is mandatory and I do this with the other RADIOS until I go through all.

Does anyone have any suggestions of what to do or any material I can give a read?

How does the HTML built with that for

<form action="X">
    <div>
        <p>Pergunta 1</p>
        <div id="1">
            <label for="">
                <input type="radio" name="1" value="1" id="1">
                <span>1</span>
            </label>
            <label for="">
                <input type="radio" name="1" value="2" id="1">
                <span>2</span>
            </label>
            <label for="">
                <input type="radio" name="1" value="3" id="1">
                <span>3</span>
            </label>
        </div>
    </div>
    <div>
     <p>Pergunta 2</p>
     <div id="2">
        <label for="">
            <input type="radio" name="2" value="1" id="2">
            <span>1</span>
        </label>
        <label for="">
            <input type="radio" name="2" value="2" id="2">
            <span>2</span>
        </label>
        <label for="">
            <input type="radio" name="2" value="3" id="2">
            <span>3</span>
        </label>
    </div>
 </div>
 <button type="submit">Enviar</button>
 </form>
  • radios need to be grouped by name, they have variable names?

  • @Ricardopunctual radios of each question have the same name, if there is another question it will have a different name for radios

  • got it. this would be simple, the answer of the Isaque already gives an example, but if you have "groups" with different names, you need a logic, for example <div id="1">.. alguns radio</div><div id="2"> outros radio </div>..., happens this?

  • @Ricardopunctual yes, he is that way. There is a DIV with the question and several possible answers that are the radios, then there may be more X Divs with questions and answers, they have the same model, but their id and name are different

  • already understood, can put a small example of html, with more than one div with answers?

  • @Ricardopunctual I will edit the question and add below

Show 1 more comment

1 answer

1


You can use the jQuery with a selector that selects all radiochecked and check whether the length is greater than 0:

    function checarRespostas() {

      var perguntas = $('[id^=pergunta]');
      perguntas.each((index, pergunta) => {
        var respostasChecadas = $(pergunta).children('label').children('input[type=radio]:checked');

        if (respostasChecadas.length == 0) {
          alert(`Responda a ${pergunta.id}`);
        }

      });

    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

    <p>Pergunta 1</p>
    <div id="pergunta_1">
        <label>
            <input type="radio" name="resposta_1" value="Opcao1"> Opção 1
            <br>
            <input type="radio" name="resposta_1" value="Opcao2"> Opção 2
        </label>
    </div>

    <p>Pergunta 2</p>
    <div id="pergunta_2">
        <label>
            <input type="radio" name="resposta_2" value="Opcao1"> Opção 1
            <br>
            <input type="radio" name="resposta_2" value="Opcao2"> Opção 2
        </label>
    </div>
    <br>

    <button onclick="checarRespostas()">Enviar</button>

The length will be 0 no case radio checked is found.

Behold: selector :checked

  • .Yes, that way I confirm that at least one is checked, but for the whole form and not individually, but there may be several questions with radio option, I need to see in each of them if at least one has been checked, if I have been passed to the next and redo the process, if I find a question that does not have a checked option I display a message in that question saying that it is mandatory

  • from what I understand the divs or other elements may be the questions, and each contains radio buttons where at least one radio button by question must be checked, this?

  • Exactly that, there is a main DIV that contains the question and the possible answers to it are radio Buttons, when sending the form I wanted to go through them and check if all are checked, putting a mandatory field message in those that are not

  • @Fábiosantos but is there anything that makes it possible to identify Divs as questions? (For example, some css class or some id like pergunta_X)

  • yes all questions have their own ID

  • @Fábiosantos edited the answer

Show 1 more comment

Browser other questions tagged

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