简体   繁体   中英

Javascript - ading a checked radio input on a variable after submitting the form

I am trying to create a rock paper scissors game.

The user choice must populate as a variable using a form with a radio button.

When I click on submit I would like to have the radio button checked to be passed as a variable. How can I get the users choice?

<div id="global">
    <h1>Jeu Pierre Feuille Ciseau</h1>
     <h3>Choisissez Pierre, Feuille ou Ciseaux:</h3>
     <div>
         <input type="radio" id="pierre" name="game" value="pierre" checked>
         <label for="pierre">Pierre</label>
    </div>
    <div>
        <input type="radio" id="feuille" name="game" value="feuille">
        <label for="pierre">Feuille</label>
    </div>
    <div>
        <input type="radio" id="ciseaux" name="game" value="ciseaux">
        <label for="pierre">Ciseaux</label>
    </div>
    <button id="submit">Submit</button>
</div>

 <script type="text/javascript">
    window.document.getElementById("submit").onclick = function(){
    }
</script>

This would be something simple with jQuery and using $("input[name='game']:checked").val()

 $(document).ready(function(){ $("input[type='button']").click(function(){ var radioValue = $("input[name='game']:checked").val(); if(radioValue){ alert("Value - " + radioValue); } }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="global"> <h1>Jeu Pierre Feuille Ciseau</h1> <h3>Choisissez Pierre, Feuille ou Ciseaux:</h3> <div> <input type="radio" id="pierre" name="game" value="pierre" checked> <label for="pierre">Pierre</label> </div> <div> <input type="radio" id="feuille" name="game" value="feuille"> <label for="pierre">Feuille</label> </div> <div> <input type="radio" id="ciseaux" name="game" value="ciseaux"> <label for="pierre">Ciseaux</label> </div> <input type="button" value="Submit"> </div>

You can do this by iterating through the radio buttons and finding the element that has been checked. From that point you can do whatever you like with the value

<div id="global">
<h1>Jeu Pierre Feuille Ciseau</h1>
 <h3>Choisissez Pierre, Feuille ou Ciseaux:</h3>
 <div>
     <input type="radio" id="pierre" name="game" value="pierre" checked>
     <label for="pierre">Pierre</label>
</div>
<div>
    <input type="radio" id="feuille" name="game" value="feuille">
    <label for="pierre">Feuille</label>
</div>
<div>
    <input type="radio" id="ciseaux" name="game" value="ciseaux">
    <label for="pierre">Ciseaux</label>
</div>
<button id="submit">Submit</button>

window.document.getElementById("submit").onclick = function(){

var radioButtons = document.getElementsByName('game');

for (var i = 0, length = radioButtons.length; i < length; i++){
  if (radioButtons[i].checked){
      // do whatever you want with the checked radio
      alert(radioButtons[i].value);

      // only one radio button can be checked so break the loop
      break;
  }
}

}

Here is a codepen to try it out: https://codepen.io/jpcobalt/pen/qBBqWyd

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM