gpt4 book ai didi

javascript - 形式和对象

转载 作者:行者123 更新时间:2023-11-28 08:07:24 25 4
gpt4 key购买 nike

我需要有关如何正确构建它的帮助。我的目标是我可以在表格上输入一个人的名字和科目,并让它显示学生在该科目上的成绩。我不知道如何处理这个问题。正如你所看到的,我不知道如何设置。关于如何攻击这个问题有什么建议吗?

HTML 代码

    <form name="reportcard">
<table>
<tr><td>Enter Student's name here:</td></tr>
<tr>
<td>Student Name:</td>
<td id="student"><input type="text" name="student1"></td>
</tr>
<tr>
<tr>
<td>Subject</td>
<td id="subject"><input type="text" name="subject1"></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Compute" onclick="callParents()"></td>
</tr>

<p id="hello"></p>

</form>

</body>

Javascript

function Student (name, art, science, english, spanish, shop, socialScience, gym) {
this.aname = name;
this.math = art;
this.science = science;
this.english = english;
this.spanish = spanish;
this.shop = shop;
this.socialScience = socialScience;
this.gym = gym;
}

var johnSmith = new Student ("John Smith", "B", "A", "A", "F", "A", "B", "A");

var janeSmith = new Student ("Jane Smith", "A", "B", "C", "A", "F" , "C", "A");

var jackSmith = new Student;
jackSmith.aname = "Jack Smith";
jackSmith.math = "A";
jackSmith.shop = "A";

function callParents () {
if (jackSmith.shop === "F") {
console.log("Call Parents!!!");
} else {
console.log("Student is doing fine in school");
}

最佳答案

好的...我对您的 html 和 JavaScript 代码进行了一些更改

在 html 中,您不需要 td 来拥有 id,而是我为输入元素添加了 Id。这是修改后的html代码:

    <body>

<form name="reportcard">
<table>
<tr>
<td>Enter Student's name here:</td>
</tr>
<tr>
<td>Student Name:</td>
<td>
<input id="student" type="text" name="student1">
</td>
</tr>
<tr>
<tr>
<td>Subject</td>
<td>
<input id="subject" type="text" name="subject1">
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="Compute" onclick="callParents();">
</td>
</tr>

<p id="hello"></p>

</form>

</body>

</html>

在 JavaScript 代码中,我没有将所有学生对象分开,而是将它们放入学生列表的单个集合中。所以循环会更容易。还添加了额外的代码来检查学生姓名和成绩。

下面是修改后的 JavaScript 代码:

    function Student(name, art, science, english, spanish, shop, socialScience, gym) {
this.aname = name;
this.math = art;
this.science = science;
this.english = english;
this.spanish = spanish;
this.shop = shop;
this.socialScience = socialScience;
this.gym = gym;
}

var johnSmith = new Student("John Smith", "B", "A", "A", "F", "A", "B", "A");

var janeSmith = new Student("Jane Smith", "A", "B", "C", "A", "F", "C", "A");

var jackSmith = new Student();
jackSmith.aname = "Jack Smith";
jackSmith.math = "A";
jackSmith.shop = "A";


var studentList = [];
studentList.push(johnSmith);
studentList.push(janeSmith);
studentList.push(jackSmith);





function callParents() {

var name = document.getElementById('student').value;

var sub = document.getElementById('subject').value;

for (var i = 0; i < studentList.length; i++) {
if (studentList[i].aname == name) {
if (studentList[i][sub] == 'F') {
console.log("Call Parents!!!");
} else {
console.log("Student is doing fine in school");
}
break;
}
}
}

这是DEMO

关于javascript - 形式和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com