Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
传递到Javascript变量的HTML select值(然后用于获取JSON)_Javascript_Html_Json_Variables_Select - Fatal编程技术网

传递到Javascript变量的HTML select值(然后用于获取JSON)

传递到Javascript变量的HTML select值(然后用于获取JSON),javascript,html,json,variables,select,Javascript,Html,Json,Variables,Select,我已经看过很多其他的堆栈问答,但我无法达到让代码正常工作所需的位置。 我觉得我想做的很简单,显然不是为了我 我有两个选择的部分,每个部分产生不同的信息是需要的 -首先是oceanVal -其次是菲什瓦尔 我希望用户选择的值随后被发送到我的javascript,作为变量用于从JSON var文件获取数据的后续过程,然后最终将其发送回oceanOutput中的我的HTML(感谢您的帮助) html: 我的提示可以工作,但html无法获取。 因此,如果你想看到它运行,只需执行提示,这就是我希望它执行的

我已经看过很多其他的堆栈问答,但我无法达到让代码正常工作所需的位置。 我觉得我想做的很简单,显然不是为了我

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

-首先是oceanVal

-其次是菲什瓦尔

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

html:

我的提示可以工作,但html无法获取。 因此,如果你想看到它运行,只需执行提示,这就是我希望它执行的操作,但单击时使用我的表单。 我还没有到达输出部分

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

JSON:


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

例如:

(函数(){
var jsonObject={
“海洋测量”:{
“海湾”:{
“鱼”:{
“海豚”:{
“名称”:“Mahi Mahi”,
“长度”:“无”,
“限额”:“每人10件或每艘船60件,以较小者为准”
},
“黑鳍金枪鱼”:{
“名称”:“黑鳍金枪鱼”,
“长度”:“未规定”,
“限制”:“所有未受管制物种的默认袋限为每天两条鱼或100磅,以较大者为准”
},
“snook”:{
“名称”:“Snook”,
“长度”:“不小于28英寸总长度(TL)或大于33英寸TL”,
“已结束”:“12月1日至2月底;5月1日至8月31日”,
“限制”:“每台收割机每天1台”,
“备注”:“当需要海水许可证时,捕捞需要Snook许可证。州规定适用于联邦水域。买卖Snook是非法的。鱼必须保持完整状态,直到上岸(头部、鳍和尾部完好无损).禁止使用抓钩和矛。禁止使用任何多钩或与活饵或死饵配合使用的方式收割。”
}
}
},
“大西洋”:{
“鱼”:{
“海豚”:{
“名称”:“Mahi Mahi”,
“长度”:“20英寸叉长”,
“限额”:“每人10件或每艘船60件,以较小者为准”
},
“黑鳍金枪鱼”:{
“名称”:“黑鳍金枪鱼”,
“长度”:“未规定”,
“限制”:“所有未受管制物种的默认袋限为每天两条鱼或100磅,以较大者为准”
},
“snook”:{
“名称”:“Snook”,
“长度”:“不小于28英寸总长度(TL)或大于32英寸TL”,
“已关闭”:“12月15日至1月31日,6月1日至8月31日”,
“限制”:“每台收割机每天1台”,
“备注”:“当需要海水许可证时,捕捞需要Snook许可证。州规定适用于联邦水域。买卖Snook是非法的。鱼必须保持完整状态,直到上岸(头部、鳍和尾部完好无损).禁止使用抓钩和矛。禁止使用任何多钩或与活饵或死饵配合使用的方式收割。”
}
}
}
}
};
var userOcean=document.getElementById(“oceanVal”);
var userFish=document.getElementById(“fishVal”);
var buttonInfo=document.getElementById(“getInfo”);
var output=document.getElementById(“oceanOutput”);
按钮info.addEventListener('click',function(){
var ocean=userOcean.options[userOcean.selectedIndex].value;
var kind=userFish.options[userFish.selectedIndex].value;
output.innerHTML=“信息:”+
“名称:”+jsonObject.ocean\u测量[ocean]['fish'][kind]。名称+”

”+ 长度:“+jsonObject.ocean\u测量[海洋][鱼类][种类].Length+”

“+ “限制:”+jsonObject.ocean\u测量[海洋][鱼类][种类].Limit+“

”; }); })();
海洋测量
你将在哪里钓鱼

请选择 海湾 大西洋 你想看看什么鱼

请选择 海豚 黑鳍金枪鱼 斯努克 显示简介
此外,JSON文件当前很小,因此尽管有if(input==选项){code…}等。。。这并不理想,因为我会在每个海湾和大西洋将JSON文件增加到40多条鱼。所以我已经修复了代码,每次我单击它执行的按钮,但它在控制台中说:导航到file:///Users//Desktop/portfolio1/oceanMeasure/index.html?oceanVal=gulf&fishVal=snook 然后页面刷新,清除控制台,清除我们所做的innerHTML,对于output.HTML,我需要使我的Get Info成为Get Info
<!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>
(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
);

})();
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."}}}}}
userOcean.options[userOcean.selectedIndex].value