If else语句在web表单中以javascript或HTML计算分数

If else statements in web forms to calculate score in javascript or HTML

本文关键字:javascript HTML 计算 语句 else web 表单 If      更新时间:2023-09-26

我试图创建一个简单的web表单来计算原始数据的分数。

例如字段A(数字):如果number小于100返回数字1,如果100-150返回数字2,如果大于100返回数字3。

字段B和C也一样

然后在提交时,表单将所有返回的数字相加,并根据结果将用户引导到不同的页面。

例如

:字段A用户输入100(因此得分1)字段B用户输入140(因此得分2)字段C用户输入200 (so scores 3)

因此,

总分为6,因此用户将被引导到得分为6的网页。

我希望这是有意义的?任何帮助都会非常感激。谢谢。

function myFunction(){
var A= document.getElementByID('field A ID').value;
var B= document.getElementByID('field B ID').value;
var C= document.getElementByID('field C ID').value;
var sum=calc(A)+calc(B)+calc(C);
switch (sum){
                    case 3:
                        //get option and change it's attr
                        break;
                    case 4:
                        //get option and change it's attr
                        break;
                    case 5:
                        //get option and change it's attr
                        break;
                    case 6:
                        //get option and change it's attr
                        break;
                    case 7:
                        //get option and change it's attr
                        break;
                    case 8:
                        //get option and change it's attr
                        break;
                    case 9:
                        //get option and change it's attr
                        break;
}
}
function calc(value){
if(value<100){
return 1;
}else if(value>100 &&value<150){
return 2;
}else{
return 3;
}
}

是这样吗?

如果你使用的是Jquery可能是这样的(不是test)

$("#BT").click(function(){
test();
})
function test(){
var score1 =0;
var score2 =0;
var score3 =0;
var finalscore =0;
if($("#yourfildA").val()!=""){
   score1 = parseInt($("#yourfildA").val());
}
if($("#yourfildB").val()!=""){
   score2 = parseInt($("#yourfildB").val());
}
if($("#yourfildC").val()!=""){
   score3 = parseInt($("#yourfildC").val());
}

if(score1==100){
finalscore +=1
}
if(score2==140){
finalscore += 2;
}
if(score3==200){
finalscore += 3;
}
    alert(finalscore);
if(finalscore ==6){
   // refirect
}
}
http://jsfiddle.net/7jcB4/10/