繁体   English   中英

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

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

我现在开始学习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>

在下拉菜单中生成数据时,我的脚本运行良好。 现在,我使用“提交”按钮来打开名为“ graph.html”的html文件的新标签页。 “ graph.html”具有以下代码

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

我的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);
});

我希望显示一个警报框,例如“ ProjectYZX”,它对应于“新建”标签页上“ index.html”文件中的下拉菜单所选选项

有人可以给我任何提示吗?

谢谢。

更新部分

    $.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);
            }   
        }
    });
    });

您可以尝试将下拉菜单的值存储在sessionStorage ,然后从新窗口中检索它:

在您的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>

在您的脚本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);
});

注意:根据您的需要,您也可以使用localStorage代替sessionStorage :)

暂无
暂无

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

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