簡體   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