gpt4 book ai didi

javascript - 初学者在将 JavaScript 链接到 case 语句时遇到问题?

转载 作者:行者123 更新时间:2023-11-28 04:57:31 25 4
gpt4 key购买 nike

我是一名初级程序员,在使用此代码时遇到困难,非常感谢任何反馈或帮助。该代码的目的是创建一个 Major Adviser,它可以计算出最好的学校。我对这个编码的问题是提交按钮没有连接并且 case 语句没有连接。请给我任何建议和反馈,告诉我你认为我可以做些什么来让它正常工作。感谢您的帮助。

<html>
<head>
<title>Major Adviser</title>
</head>
<body>
<script type=text/javascript>

function Calculate(a, b, c, d) {

var C = 0;
var SB = 0;
var S = 0;
var M = 0;
var SM = 0;
var LA = 0;
var GP = 0;

function a(){
switch (document.ZF.elements.Hobbies)
{
case value=="Math":
S + 2;
SM + 3;
LA + 1;
break;
case value=="Science":
C + 2;
SB + 2;
M + 2;
LA + 1;
GP + 2;
break;
case value=="Computer Club":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Television Club":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Community Service":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Music":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Environmentalist":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Acting":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
default:
document.writeln("not working");
}
}

function b(){

switch (document.ZF.elements.Subject);
{
case value=="Math":
S + 2;
SM + 3;
LA + 1;
break;
case value=="Science":
C + 2;
SB + 2;
M + 2;
LA + 1;
GP + 2;
break;
case value=="Social Studies":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="English":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Computers":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
case value=="Art":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
default:
document.writeln("not working");
}
}

function c(){

switch (document.ZF.elements.type_of_person);
{
case value=="Introvert":
S + 2;
SM + 3;
LA + 1;
break;
case value=="Extrovert":
C + 2;
SB + 2;
M + 2;
LA + 1;
GP + 2;
break;
case value=="Both":
SM + 1;
C + 1;
SB + 1;
M + 2;
LA + 2;
GP + 1;
break;
default:
document.writeln("not working");
}
}

function d(){

document.writeln("School of Communication and Arts: " + C);
document.writeln("School of Social and Behavioural Science: " + SB);
document.writeln("School of Science: " + S);
document.writeln("School of Science and Mathematics: " + SM);
document.writln("School of Management: " + M);
document.writln("School of Liberal Arts: " + LA);
document.writln("School of Global and Professional Programs: " + GP);

}
}
</script>

<h1 align="right"><img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo"> <img src="poughkeepsie1.JPG" width="45%" height="15%" alt="Poughkeepsie"/> <img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo"/>
</h1>

<h3 style="text-align:center">Marist Major Advisor</h3>
<form name="ZF">
First Name: <input type="text" name="firstname" size = 25 maxlength = 15> <br>
<br>
Last Name: <input type="text" name="lastname" size = 25 maxlength = 15> <br>
<br>
<p>What kind of Personality are you?</p>
<input type="radio" name="type_of_person" value="Introvert">Introvert<br>
<input type="radio" name="type_of_person" value="Extrovert">Extrovert<br>
<input type="radio" name="type_of_person" value="Both">Both<br>
<input type="radio" name="type_of_person" value="None">None<br>
<p>What are your best subjects/activities?</p>
<input type="checkbox" name="Subject" value="Math">Math<br>
<input type="checkbox" name="Subject" value="Science">Science<br>
<input type="checkbox" name="Subject" value="Social Studies">Social Studies<br>
<input type="checkbox" name="Subject" value="English">English<br>
<input type="checkbox" name="Subject" value="Computers">Computers<br>
<input type="checkbox" name="Subject" value="Art">Art<br>
<p>What activities or hobbies are you involved in?</p>
<input type="checkbox" name="Hobbies" value="Math">None<br>
<input type="checkbox" name="Hobbies" value="Science">Sports<br>
<input type="checkbox" name="Hobbies" value="Computer Club">Computer Club<br>
<input type="checkbox" name="Hobbies" value="Television Club">Television Club<br>
<input type="checkbox" name="Hobbies" value="Community Service">Community Service<br>
<input type="checkbox" name="Hobbies" value="Music">Music<br>
<input type="checkbox" name="Hobbies" value="Environmentalist">Environmentalist<br>
<input type="checkbox" name="Hobbies" value="Acting">Drama Class/Acting<br>
<p>How long are you willing to be in college for?</p>
<select>
<option value="blank"></option>
<option value="Don't Mind">Don't mind the amount of time.</option>
<option value="2Year">2 years Associates Degree</option>
<option value="4Year">4 years Bachelors Degree</option>
<option value="4+">4+ Masters Degree</option>
<option value="Doctrine">Doctrine</option>
</select><br>
<br>
<input type="button" value="Submit" onclick="Calculate()"/>
<input type="reset"Value="Clear">
</form>
</body>
</html>

最佳答案

由于您是编程新手,因此我需要指出几件事。

如果您打开任何现代浏览器并访问开发人员工具,您可能会发现语法错误。例如,如果您在 Firefox 中,只需右键单击并选择 Inspect Element with Firebug .这会让你自己开始。

修复方法如下:

  • 您需要 <!DOCTYPE html>在开头。

  • 所有分配都需要从 S + 2; 更改至 S += 2; .否则你需要做类似 S = S +2; 的事情。

  • 如前所述,您可以删除 value ==来自案例陈述。

  • 另外如前所述,您有 writln而不是 writeln .

  • 在 switch 语句的末尾有几个不能出现的分号。例如switch (document.ZF.elements.Subject);

  • 我将“提交”按钮更改为 type="button"type="submit"删除了 onclick代替 jQuery .submit()事件。

  • switch 语句没有使用 value他们只会看第一个被选中的。所以这需要一个 for循环拉出 checked值并将它们一次传递到开关函数 1。

我还清理了这个副本中的格式和语法结构,并添加了一些 jQuery。

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<meta charset="ISO-8859-1">
<title>Major Adviser</title>
</head>
<body>
<script type=text/javascript>
$( document ).ready( function() {
var C,SB,S,M,SM,LA,GP;

$("#ZF").submit(function() {
C = 0;
SB = 0;
S = 0;
M = 0;
SM = 0;
LA = 0;
GP = 0;

var hobbies = $('.hobbies');
for(var i = 0; i < hobbies.length; i++) {
if (hobbies[i].checked) {
var hobby;
hobby = hobbies[i].value;
a(hobby);
}
}

var subjects = $('.subjects');
for(var i = 0; i < subjects.length; i++) {
if (subjects[i].checked) {
var subject;
subject = subjects[i].value;
b(subject);
}
}

c();
d();
});

var a = function(hobby) {
switch (hobby) {
case "Math":
S += 2;
SM += 3;
LA += 1;
break;
case "Science":
C += 2;
SB += 2;
M += 2;
LA += 1;
GP += 2;
break;
case "Computer Club":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Television Club":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Community Service":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Music":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Environmentalist":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Acting":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
default:
document.writeln("not working");
}
};

var b = function(subject) {
switch (subject) {
case "Math":
S += 2;
SM += 3;
LA += 1;
break;
case "Science":
C += 2;
SB += 2;
M += 2;
LA += 1;
GP += 2;
break;
case "Social Studies":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "English":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Computers":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
case "Art":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
default:
document.writeln("not working");
}
};

var c= function() {

switch ($('.type_of_person').val()) {
case "Introvert":
S += 2;
SM += 3;
LA += 1;
break;
case "Extrovert":
C += 2;
SB += 2;
M += 2;
LA += 1;
GP += 2;
break;
case "Both":
SM += 1;
C += 1;
SB += 1;
M += 2;
LA += 2;
GP += 1;
break;
default:
document.writeln("not working");
}
};

var d= function() {

document.writeln("School of Communication and Arts: " + C);
document.writeln("School of Social and Behavioural Science: " + SB);
document.writeln("School of Science: " + S);
document.writeln("School of Science and Mathematics: " + SM);
document.writeln("School of Management: " + M);
document.writeln("School of Liberal Arts: " + LA);
document.writeln("School of Global and Professional Programs: " + GP);

};
});
</script>

<h1 align="right">
<img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo">
<img src="poughkeepsie1.JPG" width="45%" height="15%" alt="Poughkeepsie" />
<img src="Marist_College_2_220374.JPG" width="15%" height="15%" alt="Marist Logo" />
</h1>

<h3 style="text-align: center">Marist Major Advisor</h3>
<form name="ZF" id="ZF">
First Name: <input type="text" name="firstname" size=25 maxlength=15>
<br> <br>
Last Name: <input type="text" name="lastname" size=25 maxlength=15>
<br> <br>
<p>What kind of Personality are you?</p>
<input type="radio" name="type_of_person" class="type_of_person" value="Introvert">Introvert<br>
<input type="radio" name="type_of_person" class="type_of_person" value="Extrovert">Extrovert<br>
<input type="radio" name="type_of_person" class="type_of_person" value="Both">Both<br>
<input type="radio" name="type_of_person" class="type_of_person" value="None">None<br>
<div id='subject-div'>
<p>What are your best subjects/activities?</p>
<input type="checkbox" name="Subject" class="subjects" value="Math">Math<br>
<input type="checkbox" name="Subject" class="subjects" value="Science">Science<br>
<input type="checkbox" name="Subject" class="subjects" value="Social Studies">Social Studies<br>
<input type="checkbox" name="Subject" class="subjects" value="English">English<br>
<input type="checkbox" name="Subject" class="subjects" value="Computers">Computers<br>
<input type="checkbox" name="Subject" class="subjects" value="Art">Art<br>
</div>
<div id='hobbies-div'>
<p>What activities or hobbies are you involved in?</p>
<input type="checkbox" name="Hobbies" class="hobbies" value="Math">Math<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Science">Science<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Computer Club">Computer Club<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Television Club">Television Club<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Community Service">Community Service<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Music">Music<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Environmentalist">Environmentalist<br>
<input type="checkbox" name="Hobbies" class="hobbies" value="Acting">Drama Class/Acting<br>
</div>
<p>How long are you willing to be in college for?</p>
<select>
<option value="blank"></option>
<option value="Don't Mind">Don't mind the amount of time.</option>
<option value="2Year">2 years Associates Degree</option>
<option value="4Year">4 years Bachelors Degree</option>
<option value="4+">4+ Masters Degree</option>
<option value="Doctrine">Doctrine</option>
</select><br> <br>
<input type="submit" value="Submit"/>
<input type="reset" Value="Clear">
</form>
</body>
</html>

关于javascript - 初学者在将 JavaScript 链接到 case 语句时遇到问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366014/

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