如何运行此命令?

时间:2014-04-29 16:58:43

标签: javascript if-statement

我希望警报“请选择一个级别的条目”与警报“您必须输入考试号码”和其他两个。进入警报的级别在其他警报之前。

如果有人帮助过我,我将非常感激。谢谢!

    function validateForm()
    {
        var result = true;
        var msg="";

        var checked = null;
        var inputs = document.getElementsByName('level');

        for (var i = 0; i < inputs.length; i++) 
        {
            if (inputs[i].checked) 
            {
                checked = inputs[i];
            }
        }

        if (checked==null)
        {
            alert('Please choose a level of entry.');
            document.getElementById('radio').style.color="red";
            result=false;
        }

        else {
            var r=confirm('You have chosen '+checked.value+', do you wish to proceed? \n')
            if (r==true)
                {
                    result=true;
                }
            else
                {
                    result=false;
                }
        }


        if (document.ExamEntry.name1.value=="") {
            msg+="You must enter your name \n";
            document.ExamEntry.name1.focus();
            document.getElementById('name').style.color="red";
            result=false;
        }

        if (document.ExamEntry.subject1.value=="") {
            msg+="You must enter the subject \n";
            document.ExamEntry.subject1.focus();
            document.getElementById('subject').style.color="red";
            result=false;

        }

        if (document.ExamEntry.exam1.value=="") {
            msg+="You must enter the examination number \n";
            document.ExamEntry.exam1.focus();
            document.getElementById('exam').style.color="red";
            result=false;
        }

        if(msg=="") {
            return result;
        }

        {
            alert(msg);
            return result;
        }

    }

1 个答案:

答案 0 :(得分:1)

我理解你的意思......你应该连接字符串msg,以及......

   if (checked==null)
    {
        msg+="Please choose a level of entry.";
        document.getElementById('radio').style.color="red";
        result=false;
    }

最后放了一个:

if(msg=="") {
    return result;
} else {
    alert(msg);
    return result;
}