此代码应该帮助你!
Questions []是一个数组,其中包含所有输入字段, getElementsByName遍历所有具有相同名称的元素(例如,所有元素都具有相同名称的电子邮箱),然后找出哪个元素被检查,以及然后使用该值。
答案[]与存储的值在数组中。
for (var i = 0; i < questions.length; i++) {
//Does the question name have more than one instance? (That would make it a checkbox or radio button...
if (document.getElementsByName(questions[i]).length > 1) {
//Lets loop through all these values and find out which one is selected, and then grab the value from it.
var x = document.getElementsByName(questions[i])
for(var k=0;k
if(x[k].checked){ //This is where it loops to find the checked answer
answers[i] = x[k].value;
//alert("radio saved " + answers[i]);
}
}
//Nope, only once, so its a text field or similar
else {
answers[i] = document.getElementById("answer" + questions[i]).value;
//alert("text saved " + answers[i]);
}
}
希望这有助于!
所以在计算中使用这个(说让它双),你可以调用
var answertoyourquestion = answers[i] + answers[i];