多个单选按钮窗体、单个组无法检索值
Multiple radio button forms, single groups, can't retrieve values
我一直在网上寻找答案,但没有找到任何有用的东西。我正在单个网页上编程测验。我不是一个具有多个单选按钮组的窗体,而是多个窗体,每个窗体都有自己的单选按钮组。
每次我运行代码从每个表单中获取值时,我得到的都不超过第一组;此后,我的函数调用的第二次迭代说我的表单字段变量未定义。
调用表单值检索代码的示例:
function submit_quiz(){
var one, two, three;
one = getRadioValue(document.getElementById("answers1"), "a1");
alert(one);
one = getRadioValue(document.getElementById("answers2"), "a2");
alert(two);
one = getRadioValue(document.getElementById("answers3"), "a3");
alert(three);
}
function getRadioValue(form, name){
var val;
var i;
var f = form;
var n = name;
radios = f.elements[n];
len = radios.length;
for(i=0; i<len; i++){
if(radios[i].checked){
val = radios[i].value;
break;
}
}
return val;
}
窗体和单选按钮的代码示例:
<ol class="quiz_question">
<li id="q1">
<p>According to Sun Tzu, the study of war is what to the state?</p>
<form class="answerswer_selections" id="answers1">
<input name="a1" value="1" type="radio" />A useless endeavor to be avoided.<br />
<input name="a1" value="2" type="radio" />Too costly to be of any practical use.<br />
<input name="a1" value="3" type="radio" />A matter of life and death.<br />
<input name="a1" value="4" type="radio" />Important to the state's wealth.<br />
</form>
</li>
<li id="q2">
<p>Sun Tzu says long wars have what effect?</p>
<form class="answerswer_selections" id="answers2">
<input name="a2" value="1" type="radio" />Both the state's wealth, and the army's strength wane.<br />
<input name="a2" value="2" type="radio" />Then enemy's strenght is steadily ground into nothing.<br />
<input name="a2" value="3" type="radio" />Both sides will eventually choose a mutually agreable peace.<br />
<input name="a2" value="4" type="radio" />There will be no effect as the war will become a stalemate.<br />
</form>
</li>
<li id="q3">
<p>If your army is five times as big as your enemies, what is your best course of action?</p>
<form class="answerswer_selections" id="answers3">
<input name="a3" value="1" type="radio" />Attempt to surround and destroy the enemy's forces.<br />
<input name="a3" value="2" type="radio" />Pull back, and wait for a better opportunity to attack.<br />
<input name="a3" value="3" type="radio" />Split your forces in two, and send one to attack the enemy's rear.<br />
<input name="a3" value="4" type="radio" />Attack immediatly, unless the enemy holds some other serious advantage.<br />
</form>
</li>
找不到提交测验的按钮。因此,必须添加一个按钮并将侦听器onclick
附加到它。
也不需要传递表单id
,因为单选按钮的名称不同,document.getElementsByName
可以用来获取该名称的单选按钮。然后循环通过节点列表找到选中的按钮。
同样在你的函数submit_quiz
你正在提醒two
和three
变量,但它们实际上是未定义的。
var one, two, three; //Declared variable
one = getRadioValue(document.getElementById("answers1"), "a1"); // Assigned to one
alert(one);
//Assigned to one again but not two
one = getRadioValue(document.getElementById("answers2"), "a2");
alert(two); // alerting two but two in unassinged
// Assigned to one again ,but alerting three
one = getRadioValue(document.getElementById("answers3"), "a3");
alert(three); //three is unassigned
document.getElementById("submitQuiz").onclick =function(){
submit_quiz();
}
function submit_quiz(){
var one, two, three;
one = getRadioValue("a1");
alert(one);
two = getRadioValue("a2");
alert(two);
three = getRadioValue("a3");
alert(three);
}
function getRadioValue(name){
var val =""
var _getCheckedRadio = document.getElementsByName(name);
for(var i =0;i<_getCheckedRadio.length;i++){
if (_getCheckedRadio[i].checked ){
val = _getCheckedRadio[i].value;
}
}
return val;
}
js小提琴
相关文章:
- 将大数组(字符串和类型数组的混合物)存储到blob或文件中/从blob或文件检索大数组的最佳方式
- 尝试从csv文件中检索单个值时出现问题
- 多个单选按钮窗体、单个组无法检索值
- 如何从 Angular ng-repeat 指令中检索数组中的某些索引
- 如何使用javascript中的for循环成对检索数组元素
- 从 json 字符串中检索数组值
- 如何使用 d3 从 javascript 中的 csv 文件中检索数组
- 如何在 javascript 中检索数组中相同值的数量
- Javascript 数组未获得总和.而不是检索数组本身
- 使用$http.get检索单个记录
- 如何使用SVG解析器来呈现单个组元素
- 使用正则表达式中的单个组匹配重复模式
- 如何在AngularJS上检索单个帖子
- 检索数组和输出结果以生成一组html图像
- 使用jquery ajax从xml中检索单个记录
- jquery,如何检索一组值
- 检索数组数据从JQuery Ajax函数到javascript
- 检索数组的索引
- 如何检索数组的元素作为对象属性
- Javascript,检索数组名