Verficar buttton radio

Asked

Viewed 70 times

0

The code below only gives the first radio input someone knows how to fix it so he recognizes at least one radio input marked

$(document).ready(function(){
$(document).on('click','#teste',function(){
      if($('#area').is(":checked"))
			{
        alert('marcado');
			}
			else
			{
        alert('n marcado');
      }
      });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="area" id="area" value="vl1">

<input type="radio" name="area" id="area" value="vl2">

<input type="radio" name="area" id="area" value="vl3">

<input type="radio" name="area" id="area" value="vl4">

<button id="teste">TESTE</button>

  • 1

    The attribute id sets a single element on the page, so only the first one works. It makes no sense for you to assign the same value to id for various elements on the page.

2 answers

3


To capture the radio "checked":

$(document).on('click','#teste',function(){
     checkRadio();
});

function checkRadio() {
    var isChecked = $("input[name=area]:checked").val();
    if (!isChecked) {
        alert('Selecione algum logotipo');
        return false;
    }
}

0

$('#teste').click(function(){
  var valor = $('input[name=area]:checked').val();
  if (valor != null){
    alert ("Opção selecionada: "+valor);
  }
  else {
    alert ("Selecione uma opção!");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="radio" name="area" value="vl1">

<input type="radio" name="area" value="vl2">

<input type="radio" name="area" value="vl3">

<input type="radio" name="area" value="vl4">

<button id="teste">TESTE</button>

Browser other questions tagged

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