如何在函数中编写if语句和document.write?

时间:2019-05-31 23:32:35

标签: javascript forms function if-statement document.write

我是JavaScript的新手,因此我对这项任务感到困惑。 任务是让用户知道他的名字,并将其保存在var name中,然后进行相同的检查并将其保存在var tests中。然后,我必须检查学生考试的年级,并根据答案(1、2、3、4或5)写一条消息(如果学生获得1年级,则该消息应为“学生(姓名)获得(年级) )(在(考试)上)。“我需要用克罗地亚语来做,而对于男性和女性版本,我们有不同形式的动词,我是用代码编写的。 很抱歉,如果不清楚,但我不知道如何更好地解释。


<html lang="hr">
<head>
<meta charset="UTF-8">
<title>Task</title>
</head>
<body>

    <form>

        <p>NAME</p>
        <input id="name" type="text" name="Name" size="40" maxlength="150">
        <p>EXAM</p>
        <input id="exam" type="text" name="Exam" size="40" maxlength="150">
        <p>GENDER</p>
        <input id="gender" type="text" name="Gender" size="40" maxlength="150">
        <p>GRADE</p>
        <input id="grade" type="number" name="Grade" size="40">
        <input class="document" type="submit" onclick="task();">
        <p id="task"> </p>

        </form>
    <script>


        function task(){
            var grade= document.getElementById(grade).value;
            var name= document.getElemenetById("name");
            var exam= document.getElemenetById("exam");
            var gradetxt= [];
            var gender= document.getElemenetById("gender");


            if (grade == 1) {
                ocjenatxt = "insufficient";
            } else if (grade == 2) {
                ocjenatxt = "sufficient";
            } else (grade == 3) {
                ocjenatxt = "good";
            } else (grade== 4) {
                ocjenatxt = "very good";
            } else (grade== 5) {
                ocjenatxt = "excellent";
            }
            if (gender== "f" && grade== 1) {
                document.write("Studentica " + name+  " je na ispitu " + exam+ " pala."); /*this is for grade 1 for female (insufficient)*/
                } else if (gender == "m" && grade== 1); {
                   document.write("Student " + name + " je na ispitu " + exam+ " pao.");  /*this is for grade 1 for male (insufficient)*/
                } else (gender == "m" && grade != 1) {
                   document.write("Student " + name +  " je na ispitu " + exam + " ostvario ocjenu " + gradetxt);  /*this is for grade 2-5 for male (it should be written in words not number)*/
                } else (gender == "f" && grade != 1) {
                   document.write("Studentica " + name + " je na ispitu " + exam + " ostvarila ocjenu " + gradetxt);  /*this is for grade 2-5 for female (it should be written in words not number)*/
                }


        </script>

    </body>
</html>

1 个答案:

答案 0 :(得分:0)

getElementById()方法返回一个Element对象,该对象表示其id属性与指定字符串匹配的元素,您需要像对名称输入一样从输入中获取值。 除此之外,您还编写了名称输入标签的ID(不带双引号)。

var element = document.getElementById(id);

获得价值后,请尝试此操作,希望对您有所帮助。

var gradetext="" ;
var gendertext ="" ;

        if (grade == 1) {
            gradetext = "insufficient";
        } else if (grade == 2) {
            gradetext = "sufficient";
        } else (grade == 3) {
            gradetext = "good";
        } else (grade== 4) {
            gradetext = "very good";
        } else (grade== 5) {
            gradetext = "excellent";
        }



if (gender== "f" ) {
gendertext =" text for female ";
} else if (gender== "m" ) {
gendertext = "text for male ";
} 

document.write("Student " + name+  "grade is . " + gradetext+" gender is "+gendertext);