我现在开始学习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);
}
}
});
});
答案 0 :(得分: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
:)