Javascript - добавление проверенного радиовхода в переменную после отправки формы - PullRequest
0 голосов
/ 18 октября 2019

Я пытаюсь создать игру «Ножницы из каменной бумаги».

Выбор пользователя должен быть заполнен как переменная, используя форму с переключателем.

Когда я нажимаю «Отправить», я хочу, чтобы переключатель был отмечен как переменная. Как я могу получить выбор пользователей?

<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>

Ответы [ 2 ]

0 голосов
/ 18 октября 2019

Вы можете сделать это, просматривая переключатели и находя проверенный элемент. С этого момента вы можете делать все что угодно со значением

<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;
  }
}

}

Вот кодовое устройство, чтобы попробовать это: https://codepen.io/jpcobalt/pen/qBBqWyd

0 голосов
/ 18 октября 2019

Это было бы что-то простое с jQuery и использованием $("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>
...