来自JavaScript表单的值
我正在构建一个表单,允许计算Bishop分数: 允许用户在4个选项之间进行选择,每个选项(0厘米,1-2厘米,…)都有一个关联值 以下是表单的HTML代码:来自JavaScript表单的值,javascript,Javascript,我正在构建一个表单,允许计算Bishop分数: 允许用户在4个选项之间进行选择,每个选项(0厘米,1-2厘米,…)都有一个关联值 以下是表单的HTML代码: <form action="" id="bishopform" onsubmit="return true;"> <div> <fieldset> <legend>Bishop score</legend>
<form action="" id="bishopform" onsubmit="return true;">
<div>
<fieldset>
<legend>Bishop score</legend>
<p>(modify one field to see the score)</p>
<div id="bishopScore"></div>
<label><b>Cervical Dilatation</b></label><br/>
<label class='radiolabel'><input type="radio" name="selectedcervicaldilatation" value="cerdi1" onclick="calculateTotalBishop()"/>0 cm</label><br/>
<label class='radiolabel'><input type="radio" name="selectedcervicaldilatation" value="cerdi2" onclick="calculateTotalBishop()" checked/>1-2 cm</label><br/>
<label class='radiolabel'><input type="radio" name="selectedcervicaldilatation" value="cerdi3" onclick="calculateTotalBishop()" />3-4 cm</label><br/>
<label class='radiolabel'><input type="radio" name="selectedcervicaldilatation" value="cerdi4" onclick="calculateTotalBishop()"/>5-6 cm</label><br/>
<br/>
</fieldset>
</div>
</form>
主教分数
(修改一个字段以查看分数)
宫颈扩张
0厘米
1-2厘米
3-4厘米
5-6厘米
以下是处理选择的JavaScript代码:
var cervical_dilatation = new Array();
cervical_dilatation["cerdi1"]=0;
cervical_dilatation["cerdi2"]=1;
cervical_dilatation["cerdi3"]=2;
cervical_dilatation["cerdi4"]=3;
// getCervicalDilation() finds the points based on the answer to "Cervical Dilation".
// Here, we need to take user's the selection from radio button selection
function getCervicalDilation()
{
var cerdiPoints=0;
//Get a reference to the form id="bishopform"
var theForm = document.forms["bishopform"];
//Get a reference to the answer the user Chooses name=selectedcervicaldilatation":
var selectedCervicalDilation = theForm.elements["selectedcervicaldilatation"];
//Here since there are 4 radio buttons selectedCervicalDilation.length = 4
//We loop through each radio buttons
for(var i = 0; i < selectedCervicalDilation.length; i++)
{
//if the radio button is checked
if(selectedCervicalDilation[i].checked)
{
//we set cerdiPoints to the value of the selected radio button
cerdiPoints = cervical_dilatation[selectedCervicalDilation[i].value];
//If we get a match then we break out of this loop
//No reason to continue if we get a match
break;
}
}
//We return the cerdiPoints
return cerdiPoints;
}
function calculateTotalBishop()
{
//Here we get the Bishop Score by calling our function
//Each function returns a number so by calling them we add the values they return together
var bishopScore = 3*getCervicalDilation() + 1;
//display the result
var divobj = document.getElementById('bishopScore');
divobj.style.display='block';
divobj.innerHTML = bishopScore+"% likelihood that induction will be successful";
}
var宫颈扩张=新数组();
宫颈扩张[“cerdi1”]=0;
宫颈扩张[“cerdi2”]=1;
宫颈扩张[“cerdi3”]=2;
宫颈扩张[“cerdi4”]=3;
//getCervicalDilation()根据“宫颈扩张”的答案查找点。
//这里,我们需要从单选按钮选择中获取用户的选择
函数getCervicalDilation()
{
var cerdiPoints=0;
//获取对表单id=“bishopform”的引用
var theForm=document.forms[“bishopform”];
//获取对用户选择的答案的引用name=selectedCervicalDilation”:
var selectedCervicalDilation=form.elements[“selectedCervicalDilation”];
//此处,因为选择了4个单选按钮CervicalDilation.length=4
//我们循环浏览每个单选按钮
对于(变量i=0;i
由于任何特殊原因,我无法看到用户选择的结果
如何解决这一问题?这不起作用,因为JSFiddle在预览中如何处理脚本。因为他们使用
窗口将输入的脚本注入iframe。onload
方法,您的函数在新范围内变为私有,无法从html调用
您的代码按预期工作,因为它在SO中:
var宫颈扩张=新数组();
宫颈扩张[“cerdi1”]=0;
宫颈扩张[“cerdi2”]=1;
宫颈扩张[“cerdi3”]=2;
宫颈扩张[“cerdi4”]=3;
//getCervicalDilation()根据“宫颈扩张”的答案查找点。
//这里,我们需要从单选按钮选择中获取用户的选择
函数getCervicalDilation()
{
var cerdiPoints=0;
//获取对表单id=“bishopform”的引用
var theForm=document.forms[“bishopform”];
//获取对用户选择的答案的引用name=selectedCervicalDilation”:
var selectedCervicalDilation=form.elements[“selectedCervicalDilation”];
//此处,因为选择了4个单选按钮CervicalDilation.length=4
//我们循环浏览每个单选按钮
对于(变量i=0;i
#主教评分{
填充:10px;
字体大小:粗体;
背景色:柠檬黄;
}
主教分数
(修改一个字段以查看分数)
宫颈扩张
0厘米
1-2厘米
3-4厘米
5-6厘米
它不起作用,因为JSFiddle在预览中是如何处理脚本的。因为他们使用window.onload
方法将输入的脚本注入iframe,所以您的函数在新范围内变成private,无法从html调用
您的代码按预期工作,因为它在SO中:
var宫颈扩张=新数组();
宫颈扩张[“cerdi1”]=0;
宫颈扩张[“cerdi2”]=1;
宫颈扩张[“cerdi3”]=2;
宫颈扩张[“cerdi4”]=3;
//getCervicalDilation()根据“宫颈扩张”的答案查找点。
//这里,我们需要从单选按钮选择中获取用户的选择
函数getCervicalDilation()
{
var cerdiPoints=0;
//获取对表单id=“bishopform”的引用
var theForm=documen