Validation of sex with input radio is giving error?

Asked

Viewed 364 times

-1

I created two camps input radio for sex and put value="m" and the other value="f". I left as a standard the input with the value="m" checked (at startup is checked), but when sending to JS, even if I mark the input value="f", he returns me "m" because of checked

The input checked it’s getting in the way, but I don’t want to take it out.

This is my verification code for input radio "sex". Each input has a class() with the same name. I gave a

querySelectorAll();

and played in the variable sexo. The idea is: if the first input (((sexo[0]))) is checked, so he’s m, otherwise he is f.

if(sexo[0].checked){
    sexo1 = "m"
}else{
    sexo1 = "f";
}
  • 3

    AKU you have several open questions it would be interesting you interact and credit (of course with your will) the answers, gets a unique hand the cool is when it becomes rewarding for both sides. Rethink.

  • It has how to elaborate a [mcve]?

  • I don’t even know how to validate a sex radio input, let alone answer other people! Doesn’t mean I don’t agree, but I don’t have the knowledge to help other people yet

  • AKU of course, but, I am saying the open questions, when answered, you have to test because usually an example is put. It is in this aspect I understand also your search for knowledge and needing we are there ...

  • 1

    The @Virgilionovic is saying that you haven’t accepted any of his answers in the questions you’ve already asked back. It is important to accept an answer when it solves your problem, because it helps other people in the future who go through the same problem, then first the accepted answer, because it is the one that the author indicates solved the problem. By doing so it also gives you a reputation bonus for who you responded to, as well as motivating those who responded to you in the future because you see that your efforts have been helpful.

  • @That’s what Isac is all about in this cooperation.

Show 1 more comment

2 answers

1

Here is a functional example that I created, can base and modify your code.

$('button').click(function(){
  console.log($('input[type=radio]:checked').val());  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="sexo" value="M" checked>M</input>
<br>
<input type="radio" name="sexo" value="F" >F</input>
<br>
<button>Checar Valor</button>

0

inputs of the kind radio of the same collection must have the same name so that you can choose one or the other:

<input class="sexo" name="sexo" type="radio" value="m" checked="checked" /> masculino
<br />
<input class="sexo" name="sexo" type="radio" value="f" /> feminino

There is no problem in leaving the first marked with checked, because Javascript will normally detect which is marked:

function checa(){
   var sexo = document.querySelectorAll('.sexo');
   
   if(sexo[0].checked){
      sexo1 = "m";
   }else{
      sexo1 = "f";
   }
   alert(sexo1);
}
<input class="sexo" name="sexo" type="radio" value="m" checked="checked" /> masculino
<br />
<input class="sexo" name="sexo" type="radio" value="f" /> feminino
<br />
<br />
<input type="button" value="Verificar sexo" onclick="checa()" />

  • Remember that this way is limiting the user to use only two inputs. if ' 0 ' is not marked you deduce that the other is, I think there is a flaw in the algorithm.

  • @Mateusveloso Yes, was informed in the question, only two inputs. Unless you want to include a third sex rs

  • @Mateusveloso Your answer is good, but the question does not have the jquery tag.

  • So I communicated that it was just to be based, so in case he doesn’t use ...

Browser other questions tagged

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