gpt4 book ai didi

Javascript Switch 语句共享数组值

转载 作者:行者123 更新时间:2023-11-30 17:10:07 24 4
gpt4 key购买 nike

我正在从事一个计算机科学项目,我想创建一些 Javascript,允许用户从一个下拉菜单中选择 3 项中的 1 项,并从另一个下拉菜单中选择 3 项中的 1 项。选择是无家可归者收容所、食品银行和救世军。其次是邓肯、纳奈莫和维多利亚。如果用户选择无家可归者收容所和邓肯,它会显示该镇无家可归者的姓名和位置。

我有那部分工作,但似乎我的一些案例正在共享我的数组中的值。例如,在代表维多利亚无家可归者收容所的案例中,每次我更改数组值(例如 Relief[6])时,它也会更改纳奈莫的食品银行。他们正在分享案例。我猜这与它们的下拉值有关。无家可归者收容所是第三个选项,维多利亚是第一个 (3 + 1 = 4),纳奈莫和食品银行都是第二个选项 (2 + 2 = 4)

这是整个文档:

     <!DOCTYPE html>

<HTML>

<HEAD>
<title> Homeless Relief </title>
<script language="Javascript">

"use strict";
var relief = new Array();

relief[0]= "<H3> Homeless Shelter in Duncan </h3><H3> Warmland House </H3> <H3> 371 Festubert St 250-746-5521 </h3>";
relief[1]= "<H3> Food Bank in Duncan </h3><H3> Cowichan Valley Baskey Society </H3> <H3> 5810 Garden St 250-746-1566 </h3>";
relief[2]= "<H3> Salvation Army in Duncan </h3><H3> Community & Family Services - Cowichan Valley Ministries </H3> <H3> 280 Trans Canada Hwy 250-746-8669 </h3>";
relief[3]= "<H3> Homeless Shelter in Nanaimo </h3><H3> Samaritan House </H2> <H3> 355 Nicol St 250-753-1474 </H3>";
relief[4]= "<H3> Food Bank in Nanaimo </h3><H3> Loaves & Fishes Food Bank </H3> <H3> 1009 Farquhar St 250-754-8347 </H3>";
relief[5]= "<H3> Salvation Army in Nanaimo </h3><H3> Nanaimo Community Church </H2> <H3> 505 Eighth St 250-753-8834 </H3>";
relief[6]= "<H3> Homeless Shelter in Victoria </h3><H3> Rock Bay Landing </H3> <H3> 525 Ellice St 250-383-1951 </h3>" ;
relief[7]= "<H3> Food Bank in Victoria </h3><H3> The Mustard Seed </H3> <H3> 625 Queens Ave 250-953-1575 </H3>";
relief[8]= "<H3> Salvation Army in Victoria </h3><H3> The Salvation Army Stan Hagen Centre for Families </H3> <H3> 2695 Quadra St 250-386-8521 </H3>";

function getRelief( )
{
var location = parseFloat(document.reliefform.shelter.value);
var place = parseFloat(document.reliefform.area.value);
var together = location + place;

switch(together)

{
case 0:
document.getElementById("info").innerHTML = relief[0];

break;
case 1:
document.getElementById("info").innerHTML = relief[0];

break;
case 2:
document.getElementById("info").innerHTML = relief[0];

break;
case 3:
document.getElementById("info").innerHTML = relief[0];

break;
case 4:
document.getElementById("info").innerHTML = relief[0];

break;
case 5:
document.getElementById("info").innerHTML = relief[0];

break;
case 6:
document.getElementById("info").innerHTML = relief[0];

break;
case 7:
document.getElementById("info").innerHTML = relief[0];

break;
case 8:
document.getElementById("info").innerHTML = relief[0];

break;
}
}
</script>
</head>
<body>

<H1> Where can I find places of homeless relief in Duncan, Nanaimo, and Victoria? </H1>

<FORM NAME="reliefform">
<select name="shelter">
<option value="1" selected>Homeless Shelter</option>
<option value="2">Food Bank</option>
<option value="3">Salvation Army</option>
</select>

<select name="area">
<option value="1" selected>Duncan</option>
<option value="2">Nanaimo</option>
<option value="3">Victoria</option>
</select>

<input type="button" value="Location" name="selector" onclick="getRelief()">

</form>

<p id="info"> &nbsp; </p>

</body>
</html>

谁能解释并告诉我如何解决这个问题?

最佳答案

我发现了 3 个问题。我猜你忽略了这些。

问题 1:

将下拉列表中的值从 {1,2,3} 更改为 {0,1,2}

问题 2:

替换

var together = location + place;

var jump = document.getElementsByName("shelter")[0].length;
var together = location + (jump * place);

问题 3:

替换每一个

relief[0];

relief[together]

关于Javascript Switch 语句共享数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27197875/

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