简体   繁体   English

如何将选定的单选按钮存储在数组中?

[英]how can i store a selected radio button in an array?

I am making an online examination where questions are coming from the database through php and getting displayed through ajax.I want to get the selected radio button and store it in an array which is not happening because i am not getting the value.Everything else is working fine only i cant push the radio value in the array. 我正在进行在线检查,其中问题来自通过php的数据库并通过ajax显示。我想获取所选的单选按钮并将其存储在数组中,这是没有发生的,因为我没有得到价值。工作正常,只有我不能在数组中推送无线电值。 i can supply the php code if you need to look through the problem please help 我可以提供php代码,如果您需要检查问题,请帮助

HTML: HTML:

<button type="button" class="btn btn-info btn-md" id="mark" onclick="loadNextQues(1)">Mark for Review & Next</button>
<button type="button" class="btn btn-info btn-md">Clear Response</button>
<button type="submit" class="btn btn-info btn-md" id="save" style="position:absolute;right:20px;bottom:35px" onclick="loadNextQues(0)">Save and Next</button>
<form action="check.php">
  <button type='submit' class="btn btn-info btn-md" id="water" style="visibility:hidden;position:absolute;right:20px;bottom:35px">submit</button>

Javascript: Javascript:

 function loadNextQues(flag) {
  quesno++;
  if (flag == 1) {
    //add css to the button for review
  } else {
    //add css for the button as answered
  }
  url = "http://localhost/assignments/load-questions.php?qno=" + quesno;
  $.get(url, function(data, status) {
    response = JSON.parse(data);
    console.log(response);
    var quest = "<p>" + response.qno + " " + response.question + "</p>";
    quest += "<form>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opA +
      "'>A." +
      response.opA +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opB +
      "'>B." +
      response.opB +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opC +
      "'>C." +
      response.opC +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opD +
      "'>D." +
      response.opD +
      "<br>";
    quest += "</form>";
    document.getElementById("questBox").innerHTML = quest;
  });

  if (quesno == 15) {
    document.getElementById("mark").style.visibility = "hidden";
    document.getElementById("save").style.visibility = "hidden";
    document.getElementById("water").style.visibility = "visible";
  }
  var myArr = [];
  var Selected = document.querySelectorAll('[name="op"]:checked');
  if (Selected != null) {
    arr.push(Selected.value);
  }
  console.log(arr);
}

You could use ES6 syntax and the .map() array method. 您可以使用ES6语法和.map()数组方法。 Something like... 就像是...

var arr = Selected.map(dom => dom.value); var arr = Selected.map(dom => dom.value);

You need to iterate through the elements to get their values, also arr is not defined. 您需要遍历元素以获取其值,并且也未定义arr。

Edit to get the intended effect... 编辑以获得预期的效果...

var arr = new Array();
function loadNextQues(flag) {
  var Selected = document.querySelectorAll('[name="op"]:checked');
  if (Selected != null) {
     Selected.forEach(function(radio) {
        arr.push(radio.value);
     });
  }
  console.log(arr);

  quesno++;
  if (flag == 1) {
    //add css to the button for review
  } else {
    //add css for the button as answered
  }
  url = "http://localhost/assignments/load-questions.php?qno=" + quesno;
  $.get(url, function(data, status) {
    response = JSON.parse(data);
    console.log(response);
    var quest = "<p>" + response.qno + " " + response.question + "</p>";
    quest += "<form>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opA +
      "'>A." +
      response.opA +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opB +
      "'>B." +
      response.opB +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opC +
      "'>C." +
      response.opC +
      "<br>";
    quest +=
      '<input type="radio" name="op" value=\'' +
      response.opD +
      "'>D." +
      response.opD +
      "<br>";
    quest += "</form>";
    document.getElementById("questBox").innerHTML = quest;
  });

  if (quesno == 15) {
    document.getElementById("mark").style.visibility = "hidden";
    document.getElementById("save").style.visibility = "hidden";
    document.getElementById("water").style.visibility = "visible";
  }
  var myArr = [];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所选单选按钮的文本存储在localStorage中? - How can I store a selected radio button's text in localStorage? 如何使用 JavaScript 和 PHP 检查是否选择了单选按钮 - How can I check if a radio button is selected using JavaScript and PHP 如何根据选定的单选按钮更新选择选项? - How can I update select options based on selected radio button? 我如何知道通过 jQuery 选择了哪个单选按钮? - How can I know which radio button is selected via jQuery? 如何检查是否使用 JavaScript 选择了单选按钮? - How can I check whether a radio button is selected with JavaScript? 如何根据选定的单选按钮值传递单选按钮值? - How can i pass radio button values based on the selected one? 如果我单击选择按钮并在文本框中传递其值,如何获得所选单选按钮的值? - how can i get the value of selected radio button if i click the select button and pass its value in a textbox? 如果未选中,如何检查单选名称按钮的数组 - How can I check the array of radio name button if it is not checked 使用jQuery,如何将选择的选择值存储到数组中? - Using jQuery, how can I store the selected values of a select into an array? 一个按钮如何根据选择的单选按钮显示不同的结果 - How can a button display different results depending on radio button selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM