2014-02-15 36 views
0

我的那一刻開始學習JavaScript,我有下面的代碼在index.html文件來生成一個下拉菜單打開新Tab提交按鈕後,並得到下拉菜單的數據

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

回答

0

您可以嘗試到您的下拉菜單中的值存儲在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

+0

非常感謝,它的偉大工程:d – user3063604

+0

@ user3063604:你的隊友的歡迎! ;)不要忘記驗證答案:D – Littm

+0

代碼有一個小錯誤,如果我在開始時選擇第一個選項,它總會給我一個空的警告框。但是,如果我選擇第二個選項或第三個選項,然後回來選擇第一個選項,則代碼完美工作。你有什麼主意嗎? – user3063604

相關問題