使用javascript检查是否选择了单选按钮,并根据选择返回特定答案
Using javascript to check if a radio button is selected and return a specific answer based on the selection
我正在以最干净的方式寻找一个简单的是或否问题,并测试选择了哪个答案。
例如,在这种情况下,如果"是",我希望它将值"继续"返回到指定区域(一个三列表,第一列有一个问题,第二列有单选按钮,第三列更新并显示答案)。
到目前为止,我的JS代码是:
<script type="text/javascript">
var answer = 'place-holder';
var user_input;
function checkForm()
{
var substanceab = document.getElementById('Sub');
for (i = 0; i < substanceab.length; i++)
{
if(substanceab.length[i].checked)
{
user_input = substanceab.length[i].value;
}
}
if (user_input ="yes")
{
return answer = "Continue";
}
else if (user_input ="no")
{
return answer = "Provide Alternate Referral";
}
else
{
return;
}
};
function writeAns()
{
document.getElementById('answerwrite').innerHTML = checkForm[user_input];
};
</script>
正文(减去实际问题):
<table class="table table-bordered">
<tr>
<td width="58%"><center><strong>Required:</strong></center></td>
<td width="19%"></td>
<td width="23%"><center><u><strong>___________</strong></u></center></td>
</tr>
<tr>
<td> <span class="sub depend"> <strong><i>_________</i> </strong></span></td>
<td> <span class="sub depend ans">
<form name="Substance">
<label class="radio inline">
<input type="radio" value="yes" name="substance" onclick="writeAns()">
yes </label>
<label class="radio inline">
<input type="radio" value="no" name="substance" onclick="writeAns()">
no </label> </form></span></td>
<div id="answerswerwrite"></div>
<script type="text/javascript">
document.write("<td>" + writeAns() + "</td>")
</script>
</tr></table>
好吧,修复了"funk",但正如我所说,更习惯于java而不是javascript,语法更加强硬。我同意,我只是用ID的东西来尝试用不同的方法来实现这一点,但从来没有。现在有了一些建议,它只是让我到处都不确定。虽然我同意这最终会转向jquery,但我不知道如何使用它,因此首先要用javascript来解决这个问题。
几件事:
document.getElementByID()
将始终返回第一个元素,因为ID应该是唯一的。相反,使用:
document.getElementsByName('substance');
接下来,在循环中,您使用.length属性不正确地引用了数组的成员。试试这个:
for (i = 0; i < substanceab.length; i++)
{
if(substanceab[i].checked)
{
user_input = substanceab[i].value;
}
}
最后,在javascript中,'='operator始终是一个赋值语句。要进行比较,请始终使用"==":
if (user_input == "yes")
{
return answer = "Continue";
}
else if (user_input =="no")
{
return answer = "Provide Alternate Referral";
}
else
{
return;
}
此外,尽可能避免全局变量。这是经过一点重构的工作代码:
<input type="radio" value="no" id= "Sub" name="substance" onclick="writeAns()">
function checkForm()
{
var user_input;
var substanceab = document.getElementsByName('substance');
for (i = 0; i < substanceab.length; i++)
{
if(substanceab[i].checked)
{
user_input = substanceab[i].value;
}
}
if (user_input == "yes")
{
return "Continue";
}
else if (user_input =="no")
{
return "Provide Alternate Referral";
}
else
{
return;
}
};
function writeAns()
{
document.getElementById('answerwrite').innerHTML = checkForm();
};
如果您使用的是JQuery,则非常简单。
确保您的元素具有相同的"名称"属性并使用:
var RadioGroupResult = $('input[name="MyRadioGroupNameHere"]:checked').val();
漂亮又简单。
相关文章:
- 如何将返回的值应用于多个不同位置的多个选择器
- JQuery UI日期选择器:在选择出发日期时自动弹出返回日期
- jQuery自动完成触发器在选择时返回
- 如何使HTML5颜色选择器返回颜色名称而不是颜色代码
- 如何将日期选择器返回的日期格式转换为其他格式
- 当选择器返回多个元素时,使用 JQuery 的 .attr()
- 选择一个从 mysql 返回选中/未选中复选框的对象
- 有没有办法将选择器返回到元素?是$(这个)还是其他方法
- 选择选项的值将在表单上返回 erro 时丢失
- Why.closest(选择器)返回多个值
- NodeJS + mysql:选择返回单个对象的查询
- Javascript 选择返回错误的偏移量
- 不要在jquery自动完成中选择返回的项目
- jQuery选择返回无效的选择数据
- 下拉字段选择/返回null剑道ui网格mvc
- 使用javascript检查是否选择了单选按钮,并根据选择返回特定答案
- 'hasClass'和'是'?另外,也可以选择返回Jquery元素
- 创建一个基于下拉选择返回报价的表单
- 引导角模式对话框:不能从选择返回值
- Knockout Javascript选择返回Object对象