Validate if one of the radiobuttons are selected

0

I have this code:

$(document).ready(function()
    {
    $("#myBtn").click(function () {  
        if( $("#formulario input[name='radio']:radio").is(':checked')) {  
            $("#formulario").submit();  
            } else{  
                alert("Selecciona un metodo de pago.");  
                }  
    });
 });

The problem is that the alert is displayed when you hit the button to make the record while one is selected. It works well up to that point.

Thanks for the help.

    
asked by Diego Lopez 25.11.2017 в 23:59
source

2 answers

0

In my case it was just two radio buttons. In this case, it is enough to validate that one of the two is checked. Because doing it in the ways I found on the Internet validated that everyone was checked not one of those options.

Here my code:

 $(document).ready(function(){  

    $("#myBtn").click(function() {  
        if($("#boton1").is(':checked') || $("#boton2").is(':checked')) {  

        } else {  
            alert("Seleccione un metodo de pago.");  
        }  
    });  

});
    
answered by 26.11.2017 / 15:14
source
0

I hope this helps you greetings

$("#enviar").click(function(){
  if( $("#formulario input[type='radio']").is(':checked')) {  
            $("#formulario").submit();  
            alert("enviar formulario")
            } else{  
                alert("Selecciona un metodo de pago.");  
                }  
})

$(document).ready(function()
    {
    $("#myBtn").click(function () {  
        
    });
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="formulario">
  <input id="radio1" type="radio" name="radios" value="">
  <label for="radio1">pago1</label>
  <input id="radio2" type="radio" name="radios" value="">
  <label for="radio2">pago2</label>


<input id="enviar" type="submit" name="" value="Enviar">
</div>
    
answered by 26.11.2017 в 01:00