简体   繁体   中英

How to get input text value next to unchecked radio buttons?

I have a simple form for posting multiple choice questions. But I'm stuck on this issue. I have 4 input groups with radio button and input next to it. I'm able to get the value of input text which is next to checked radio button but I also want to get input values of all remaining 3 inputs which are next to unchecked radio buttons. Any help would be appreciated. Thanks.

form.html

<div class="input-group">
                  <div class="input-group-prepend">
                    <div class="input-group-text">
                      <input
                        type="radio"
                        aria-label="Select radio button for correct option"
                        name="option"
                        required
                      />

                    </div>
                  </div>
                  <input
                    type="text"
                    class="form-control"
                    id="optionB"
                    placeholder="Option B"
                    required
                  />
                </div>

script.js

var correctChoice = $("input:radio:checked")
    .closest(".input-group")
    .find('input[type="text"]')
    .val();

  var uncheckedRadioButtons = $('input[type="radio"]:not(:checked)');

  for (var i = 0; i < uncheckedRadioButtons.length; i++) {
    console.log(uncheckedRadioButtons[i]);
  }

I managed to get this working:

var options = ["optionA", "optionB", "optionC", "optionD"];
  var question = document.getElementById("question").value;
  var correct_answer = $("input:radio:checked")
    .closest(".input-group")
    .find('input[type="text"]')
    .val();

  var correctChoiceInputId = $("input:radio:checked")
    .closest(".input-group")
    .find('input[type="text"]')
    .attr("id");

  var wrong_answers = [];

  for (var i = 0; i < options.length; i++) {
    if (correctChoiceInputId !== options[i]) {
      wrong_answers.push(document.getElementById(options[i]).value);
    }
  }

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