HTML 选择值传递到 Javascript var(然后用于获取 JSON)

2023-11-30

我已经查看了许多其他堆栈问答,但无法到达我需要让我的代码正常工作的地方。 我觉得我想做的事情很简单,显然不适合我。

我有两个选择部分,每个部分产生所需的不同信息。

-首先是 oceanVal

-第二个是fishVal

我希望用户选择的值发送到我的 javascript,在以下从 JSON var 文件获取数据的过程中用作变量,然后最终将其发送回 oceanOutput 中的 HTML(感谢您的帮助) )。

html:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>OceanMeasure</title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>
    <body>
        <h1>Ocean Measure</h1>
        <div>
            <p>Where will you be fishing?</p>
            <form>
                <select name="oceanVal" id="oceanVal">
                    <option value="" disabled="disabled" selected="selected">Please select</option>
                    <option value="gulf">Gulf</option>
                    <option value="atlantic">Atlantic</option>
                </select>
            </div>
            <div>
                <p>What fish would you like to look up?</p>
                <select name="fishVal" id="fishVal">
                    <option value="" disabled="disabled" selected="selected">Please select</option>
                    <option value="dolphin">Dolphin</option>
                    <option value="blackfin tuna">Blackfin Tuna</option>
                    <option value="snook">Snook</option>
                </select>
            </div>
            <button>Get Info</button>
        </form>
        <div id="oceanOutput"></div>

        <script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
        <script type="text/javascript" src="js/data.json"></script>
        <script type="text/javascript" src="js/main.js"></script>
    </body>
</html>

javascript:

(function(){
// var userOcean = prompt("What ocean will you be fishing in?");
// var userFish = prompt("What fish would you like to look up?");

var userOcean = document.getElementById("oceanVal");
var userFish = document.getElementById("fishVal");



console.log(
  "\n\nfish:  "+jsonObject.ocean_measure[userOcean].fish[userFish].name+
  "\n\nlength:  "+jsonObject.ocean_measure[userOcean].fish[userFish].length+
  "\n\nclosed:  "+jsonObject.ocean_measure[userOcean].fish[userFish].closed+
  "\n\nlimit:  "+jsonObject.ocean_measure[userOcean].fish[userFish].limit+
  "\n\nremarks:  "+jsonObject.ocean_measure[userOcean].fish[userFish].remarks
);

})();

我的提示有效,但我的 html 不行。 因此,如果您想看到它的运行,只需按照提示操作,这就是我想要它做的事情,但在单击时使用我的表单。 我还没有到达输出部分。

(不确定是否需要,但这是我的 JSON - 我将缩小它。)

JSON:

var jsonObject = {"ocean_measure":{"gulf":{"fish":{"dolphin":{"name":"Mahi-mahi","length":"none","limit":"10 per person or 60 per vessel whichever is less"},"blackfin tuna":{"name":"Blackfin Tuna","length":"not regulated","limit":"The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"},"snook":{"name":"Snook","length":"Not less than 28 inches total length (TL) or more than 33 inches TL","closed":"Dec. 1-end of February; May 1-Aug. 31","limit":"1 per harvester per day","remarks":"Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."}}},"atlantic":{"fish":{"dolphin":{"name":"Mahi-mahi","length":"20 inches fork length","limit":"10 per person or 60 per vessel whichever is less"},"blackfin tuna":{"name":"Blackfin Tuna","length":"not Regulated","limit":"The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"},"snook":{"name":"Snook","length":"Not less than 28 inches total length (TL) or more than 32 inches TL","closed":"Dec. 15 to Jan. 31, June 1 to Aug. 31","limit":"1 per harvester per day","remarks":"Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."}}}}}

您需要传递选定的选项值而不是对象

Eg:

userOcean.options[userOcean.selectedIndex].value

(function(){

var jsonObject = {
  "ocean_measure": {
    "gulf": {
      "fish": {
        "dolphin": {
          "name": "Mahi-mahi",
          "length": "none",
          "limit": "10 per person or 60 per vessel whichever is less"
        },
        "blackfin tuna": {
          "name": "Blackfin Tuna",
          "length": "not regulated",
          "limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
        },
        "snook": {
          "name": "Snook",
          "length": "Not less than 28 inches total length (TL) or more than 33 inches TL",
          "closed": "Dec. 1-end of February; May 1-Aug. 31",
          "limit": "1 per harvester per day",
          "remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
        }
      }
    },
    "atlantic": {
      "fish": {
        "dolphin": {
          "name": "Mahi-mahi",
          "length": "20 inches fork length",
          "limit": "10 per person or 60 per vessel whichever is less"
        },
        "blackfin tuna": {
          "name": "Blackfin Tuna",
          "length": "not Regulated",
          "limit": "The default bag limit for all unregulated species is two fish or 100 pounds per day, whichever is more"
        },
        "snook": {
          "name": "Snook",
          "length": "Not less than 28 inches total length (TL) or more than 32 inches TL",
          "closed": "Dec. 15 to Jan. 31, June 1 to Aug. 31",
          "limit": "1 per harvester per day",
          "remarks": "Snook permit required for harvest when saltwater license required. State regulations apply in federal waters. Illegal to buy or sell snook. Fish must remain in whole condition until landed ashore (heads, fins, and tails intact). Snatch hooks and spearing prohibited. Harvest prohibited by or with the use of any multiple hook in conjuction with live or dead bait."
        }
      }
    }
  }
};

var userOcean = document.getElementById("oceanVal");
var userFish = document.getElementById("fishVal");
var buttonInfo = document.getElementById("getInfo");
var output = document.getElementById("oceanOutput");

buttonInfo.addEventListener('click', function() {
  var ocean = userOcean.options[userOcean.selectedIndex].value;
  var kind = userFish.options[userFish.selectedIndex].value;
  output.innerHTML = "<h1>Info:</h1>"+
    "<p>Name: "+jsonObject.ocean_measure[ocean]['fish'][kind].name+"</p>"+
    "<p>Length: "+jsonObject.ocean_measure[ocean]['fish'][kind].length+"</p>"+
    "<p>Limit: "+jsonObject.ocean_measure[ocean]['fish'][kind].limit+"</p>";
});
})();
       <h1>Ocean Measure</h1>
        <div>
            <p>Where will you be fishing?</p>
            <form>
                <select name="oceanVal" id="oceanVal">
                    <option value="" disabled="disabled" selected="selected">Please select</option>
                    <option value="gulf">Gulf</option>
                    <option value="atlantic">Atlantic</option>
                </select>
            </div>
            <div>
                <p>What fish would you like to look up?</p>
                <select name="fishVal" id="fishVal">
                    <option value="" disabled="disabled" selected="selected">Please select</option>
                    <option value="dolphin">Dolphin</option>
                    <option value="blackfin tuna">Blackfin Tuna</option>
                    <option value="snook">Snook</option>
                </select>
            </div>
            <button id="getInfo">Get Info</button>
        </form>
        <div id="oceanOutput"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

HTML 选择值传递到 Javascript var(然后用于获取 JSON) 的相关文章

  • 在 Javascript 中隐藏按钮

    在我最新的程序中 有一个按钮 单击时会显示一些输入弹出框 这些框消失后 如何隐藏按钮 You can set its visibility财产 http www w3schools com cssref pr class visibilit
  • 通过标记或JS强制下载

    假设我在 CDN 来自 Rackspace 的云文件 上有一个文件 以及一个包含该文件链接的静态 html 页面 有什么方法可以强制下载此文件 以防止它在浏览器中打开 例如 mp3 我们可以让我们的服务器读取该文件并将相应的标头设置为 he
  • 如何替换 CSS 表达式

    我有一个用于时间表条目的旧 ASP Web 应用程序 其中充满了 CSS 表达式 它们出现在 CSS 文件中 ApptPage position relative height expression Math max document bo
  • PHP、MySQL 验证故障且搜索不起作用?

    我创建了一个小的注册粘性表格 一切工作正常 但如果我输入任何错误的值 例如姓名中的数字 年龄中的字母甚至错误的电子邮件格式 那么数据仍然保存在数据库中 我无法找出验证问题 另外一个是搜索选项 每当我在搜索框中输入任何名字或姓氏时 它都应该显
  • CSS @import 及其顺序

    是否可以使用 importone像这样的 css 文件 import file1 some css here import file2 chrome 无法识别上述第二个导入 但这可以工作 import file1 import file2
  • 如何在 Google 地图上旋转叠加图像?

    我正在尝试将一系列叠加层放置到 Google 地图上 我正在跟随地面覆盖层的示例代码 https developers google com maps documentation javascript examples groundover
  • 重复 Pinterest Facebook 邀请功能

    I m trying to duplicate Pinterest s Invite Friends functionality In case you haven t seen what it looks like it looks li
  • 将 WPF 属性数据绑定到变量

    我有一个 WPF 4 和 VB net 2010 中的项目 我想将媒体元素的音量属性绑定到另一个模块中的变量 请注意 变量的数据类型正确 因此无需转换 我该怎么做呢 首先 确保您的后台代码实现了 INotifyPropertyChanged
  • javascript 中的工厂模式与构造函数模式[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我看到了关于 javascript 设计模式的教程 虽然教程很好 但它给我留下了很少的问题 正如我所见 工厂和构造函数产生相同的结果
  • 提交外部有 INPUT 的表单,并修改值后

    我有一个form 有几个
  • 解析字符串:提取单词和短语 [JavaScript]

    我需要在以空格分隔的术语列表中支持确切的短语 用引号引起来 因此 用空格字符分割相应的字符串已经不够了 Example input foo bar lorem ipsum baz output foo bar lorem ipsum baz
  • Lighthouse 多个 URL

    我需要对一个网站进行全面审核 但我想知道是否有任何方法可以让 Lighthouse 做到这一点 我知道他们不支持完整的站点审核或多个 URL 但我发现可以使用 bash 脚本来完成 因此 我将不胜感激对此案的任何帮助 或者您可能会推荐任何灯
  • JavaScript 模板文字稍后替换变量(在知道值之前)

    是否可以在已知值之前以某种方式使用模板文字 JS 模板文字的每个演示都如下所示 var name John var s Hello name 但在现实世界中 模板是在我们知道变量值之前定义的 某处定义了模板 在页面加载时 var s Hel
  • 如何使用 JQuery 创建新的 img 标签,并使用 JavaScript 对象中的 src 和 id?

    我从基本意义上了解 JQuery 但对它绝对是新手 并且怀疑这很容易 我在 JSON 响应中获得了图像 src 和 id 转换为对象 因此在 responseObject imgurl 和 responseObject imgid 中获得了
  • 如何拦截javascript中innerHTML的变化?

    我需要拦截网页内单元格内容的任何更改 以下代码显示 addEventListener 不起作用 function modifyText alert var el document getElementById mycell el inner
  • Promise链基本问题

    我正在尝试理解 Promise 我创建了一些有效的承诺链 而另一些则无效 我已经取得了进步 但显然缺乏基本概念 例如 以下承诺链不起作用 这是一个愚蠢的例子 但说明了问题 我正在尝试在链中使用 Node 的函数 randomBytes 两次
  • 单击即可切换背景颜色和过渡

    这看起来应该很容易 但我真的找不到办法做到这一点 动画 http doir ir css gif http doir ir css gif 当您单击这些相应的链接时 我需要更改和过渡页面的背景颜色 我见过的最接近触发这种类型转换的事情是 仅
  • 如何从索引文件迭代多个导入的模块

    我有一个名为Polygons我在那里创建了一个index jsfile 以导出目录中的所有文件 它看起来像这样 export default as europe from europe export default as northAmer
  • 在用户单击之前图像不会绘制在画布上?

    我使用执行类似以下操作的函数绘制几张图像 context drawImage img width 2 1 height 2 1 width height 我读过 我需要等待图像加载后才能绘制它 如下所示 img onload functio
  • ASP.NET MVC3 Ajax.ActionLink - 条件确认对话框

    我有一个 Ajax ActionLink 仅当满足某些条件 用户有未保存的更改 时 我才希望显示一个确认对话框 我创建了一个 JavaScript 函数 它根据需要显示确认对话框 并根据响应返回 true 或 false 我将其绑定到 Ac

随机推荐