[英]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.