简体   繁体   English

在“提交”按钮后打开新标签并获取下拉菜单的数据

[英]Open new tab after submit button and get data of the dropdown menu

I start learning JavaScript at the moment, I have the following code to generate a dropdown menu in a index.html file 我现在开始学习JavaScript,我有以下代码在index.html文件中生成一个下拉菜单

<select id="secondbox" name="project">
    <option selected value="">---Generate---</option>
    <script src="myjs.js"> </script>
</select>

<input value="Submit" id="submit" type="submit"> </input>
<script>
// new tab here
$("input[type='submit']").click(function(){
    window.open('graph.html');
});
</script>

My script works well when generates the data within the drop-down menu. 在下拉菜单中生成数据时,我的脚本运行良好。 Now I am using submit button to open a New Tab with html file called "graph.html". 现在,我使用“提交”按钮来打开名为“ graph.html”的html文件的新标签页。 The "graph.html' have the following code “ graph.html”具有以下代码

    <div id="graph"> </div>
    <script src="getjs.js"> </script>

My JavaScript file is 'getjs.js' with a very basic function 我的JavaScript文件是具有基本功能的“ getjs.js”

$(function(){
    // this code works and display TEST NEW PAGE
    //$("#graph").append("<strong>TEST NEW PAGE</strong>");

    // not working
    var searchName=$("select[name='project']").val();
    alert(searchName);
});

I want an alert box appears such as "ProjectYZX' corresponding to the drop-down menu selected options in my 'index.html' file on my New Tab Page 我希望显示一个警报框,例如“ ProjectYZX”,它对应于“新建”标签页上“ index.html”文件中的下拉菜单所选选项

Could anyone be able to give me any hints? 有人可以给我任何提示吗?

Thank you. 谢谢。

Updating part 更新部分

    $.getJSON("mydata.json",function(data){
    var searchName=sessionStorage.getItem("myval");

    $.each(data,function(index,obj){
        for(var i in data.names){
            if(searchName==data.names[i].Name){
                alert(data.names[i].Name);
            }   
        }
    });
    });

You may try to store the value of your drop-down menu in sessionStorage , then retrieve it from your new window: 您可以尝试将下拉菜单的值存储在sessionStorage ,然后从新窗口中检索它:

In your index.html : 在您的index.html

<select id="secondbox" name="project">
    <option selected value="">---Generate---</option>
    <script src="myjs.js"> </script>
</select>

<input value="Submit" id="submit" type="submit"> </input>

<script>
$(function() {
    // Initialize the drop-down menu's value in sessionStorage
    sessionStorage.setItem("my_select_value", $("#secondbox").val());

    // Storing the drop-down menu's value in sessionStorage on change
    $("#secondbox").change(function() {
        sessionStorage.setItem("my_select_value", $(this).val());
    });


    // new tab here
    $("input[type='submit']").click(function(){
        window.open('graph.html');
    });
});
</script>

In your script getjs.js : 在您的脚本getjs.js

$(function(){
    // Retriving the drop-down menu's value from the sessionStorage
    var my_select_value = sessionStorage.getItem("my_select_value");
    alert(my_select_value);
});

Note: depending on your needs, you could also use localStorage instead of sessionStorage :) 注意:根据您的需要,您也可以使用localStorage代替sessionStorage :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM