生成风险评分的表格

时间:2015-12-04 16:04:59

标签: javascript html

我正在创造一个冗长的形式。如果您符合条件(勾选标记),则有5个不同的部分供您标记。总共可能有50个问题。每个部分具有不同的重量/点(第1部分各1分,第2部分各3分)。如何显示总分并且我的脚本计算看起来不错?对不起,我对此很新。没有包括整个表格,因为它很长。

<script language="JavaScript1.1" type="text/javascript">
/* <![CDATA[ */

var calctxt = '';
var xmltxt = '';
var htmtxt = '';

function DVTScoreWells_fx() {
with(document.DVTScoreWells_form){

Score = 0.0;
doCalc = true;

if (1PQ1.checked){
Score = Score + 1;
}
if (1PQ2.checked){
Score = Score + 1;
}
if (1PQ3.checked){
Score = Score + 1;
}
if (1PQ4.checked){
Score = Score + 1;
}
if (1PQ5.checked){
Score = Score + 1;
}
if (1PQ6.checked){
Score = Score + 1;
}
if (1PQ7.checked){
Score = Score + 1;
}
if (1PQ8.checked){
Score = Score + 1;
}
if (1PQ9.checked){
Score = Score + 1;
}
if (1PQ10.checked){
Score = Score + 1;
}
if (1PQ11.checked){
Score = Score + 1;
}
if (1PQ12.checked){
Score = Score + 1;
}
if (1PQ13.checked){
Score = Score + 1;
}
if (1PQ14.checked){
Score = Score + 1;
}
if (1PQ15.checked){
Score = Score + 1;
}
if (2PQ1.checked){
Score = Score + 2;
}
if (2PQ2.checked){
Score = Score + 2;
}
if (2PQ3.checked){
Score = Score + 2;
}

cctotal.value = Score;

if (doCalc){

rrclr();
}
}
}
</script>

<form name="DVTScoreWells_form" id="DVTScoreWells_form" action="#" onsubmit="return false;" onreset="rrclr();">
<br><br>
<p>Are you at risk for DVT?  Fill out this Risk Assessment and take a look.</p><br><br>
</div>
</div>
</div>
</div>
</div>
</td>
</tr>
</tbody>
</table>
<table bgcolor="#FFFFFF" cellpadding="200" cellspacing="200" class="bodyContainer">
<tbody>
<tr>
<td class="leftSidebar" sectionid="leftSidebar" valign="top">
<div>
<div>
<div class="title" style="text-align:left">
<div class="title" contentid="title" style="text-align: left;">
<div>
<div>
<span style="text-decoration: none;"><span style="font-family: Comic Sans MS; font-size: 18pt;">Each Risk Factor 

Represents 1 Point</span></span>
</div>
<div>
&nbsp;
</div>
</div>
</div>
</div>
</div>
</div>
<div>
<table class="infusion-field-container" style="width:270;">
<tbody>
<tr>

<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ1" name="1PQ1" type="checkbox" onclick="DVTScoreWells_fx();" 

/>
<label for="1PQ1">Age 41 - 59</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ2" name="1PQ2" type="checkbox" onclick="DVTScoreWells_fx();" 

/>
<label for="1PQ2">Minor surgery planned</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ3" name="1PQ3" type="checkbox" onclick="DVTScoreWells_fx

();"/>
<label for="1PQ3">History of prior major surgery</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ4" name="1PQ4" type="checkbox" onclick="DVTScoreWells_fx

();"/>
<label for="1PQ4">Varicose veins</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ5" name="1PQ5" type="checkbox" onclick="DVTScoreWells_fx

();"/>
<label for="1PQ5">History of inflammatory bowel disease</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ6" name="1PQ6" type="checkbox" onclick="DVTScoreWells_fx

();"/>
<label for="1PQ6">Swollen legs (current)</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ7" name="1PQ7" type="checkbox" onclick="DVTScoreWells_fx

();"/>
<label for="1PQ7">Obesity (BMI >30)</label>
</td>
</tr><tr>
</td>
</tr><tr>
<td class="infusion-field-input-container" style="width:270px;">
<input class="infusion-field-input-container" id="1PQ8" name="1PQ8" type="checkbox" onclick="DVTScoreWells_fx

();"/>

1 个答案:

答案 0 :(得分:2)

老实说,当前代码接近此方式的方式很容易通过拼写错误或遗漏而出错,同时也很难维护。

我的主要建议是利用包含运行总计的分数为id的div,让每个复选框调用一个函数,将其值作为调整得分的innerHTML的参数,例如: HTML:
Your Risk is <span id='risklevel'>Low</span> (<span id='score'>0</span>)
<input type='checkbox' onchange='updateScore(this,3);' />

JS:

function updateScore(ele,val){
    var score = document.getElementById('score');
    var riskLevel = document.getElementById('risklevel');

    var curScore = parseFloat(score.innerHTML);
    curScore += (ele.checked ? val : -val);
    score.innerHTML = curScore;

    if(curScore <= 1){
        riskLevel.innerHTML = "Low";
    }else if(curScore <= 2){
        riskLevel.innerHTML = "Moderate";
    }else if(curScore <= 4){
        riskLevel.innerHTML = "High";
    }else{
        riskLevel.innerHTML = "Very High";
    }
}


如果你想坚持使用一次性计算方法......

使用本地dom-searchable方式标记复选框(我建议使用数据前缀的类 - (例如: data-riskFactorSet )),为每个复选框赋予data- *属性(如as data-riskvalue )并让你的js迭代所有checkboxen,结合data- *属性的int / float值。

注意:因为它是一个不太受欢迎的构造...... (ele.checked ? val : -val)使用三元运算符。可以把它想象成一个内联的 if-then-else 语句,并且规定true和false部分的返回类型必须相同。它的工作原理如下:
test ? ifTrue : ifFalse