javascript子窗口向父窗口传值实例。
方案1:
parent01.html
- <!doctype html public ”-//w3c//dtd html 4.0 transitional//en”>
- <html>
- <head>
- <title>父窗口</title>
- <meta name=”generator” content=”editplus”>
- <meta name=”author” content=”">
- <meta name=”keywords” content=”">
- <meta name=”description” content=”">
- </head>
- <script type=”text/javascript”>
- function addwindow() {
- var href = ”/demo/window/son01.html”;
- window.open(href, ”newwindow”, ”height=200,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no, location=no,status=no”);
- }
- function add(id, textvalue) {
- document.getElementById(id).value = textvalue;
- }
- </script>
- <body>
- <input id=”newId” type=”text”><br>
- <input type=”button” value=”addwindow” onclick=”addwindow()”>
- </body>
- </html>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>父窗口</title> <meta name="generator" content="editplus"> <meta name="author" content=""> <meta name="keywords" content=""> <meta name="description" content=""> </head> <script type="text/javascript"> function addwindow() { var href = "/demo/window/son01.html"; window.open(href, "newwindow", "height=200,width=400,toolbar=no,menubar=no,scrollbars=no,resizable=no, location=no,status=no"); } function add(id, textvalue) { document.getElementById(id).value = textvalue; } </script> <body> <input id="newId" type="text"><br> <input type="button" value="addwindow" onclick="addwindow()"> </body> </html>
son01.html
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.0 Transitional//EN”>
- <HTML>
- <HEAD>
- <TITLE>子窗口</TITLE>
- <META NAME=”Generator” CONTENT=”EditPlus”>
- <META NAME=”Author” CONTENT=”">
- <META NAME=”Keywords” CONTENT=”">
- <META NAME=”Description” CONTENT=”">
- </HEAD>
- <script type=”text/javascript”>
- function test() {
- var textValue;
- for (var i = 0; i < document.getElementById(“newId”).length; i++) {
- if (document.getElementById(“newId”).options[i].selected) {
- textValue = document.getElementById(“newId”).options[i].text;
- break;
- }
- }
- window.opener.add(“newId”, textValue);
- self.close();
- }
- </script>
- <BODY>
- <form id=”form1″>
- <select id=”newId”>
- <option value=”1″>1</option>
- <option value=”2″>2</option>
- <option value=”3″>3</option>
- </select>
- <input type=”button” value=”submit” onclick=”test()”>
- </form>
- </BODY>
- </HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>子窗口</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <script type="text/javascript"> function test() { var textValue; for (var i = 0; i < document.getElementById("newId").length; i++) { if (document.getElementById("newId").options[i].selected) { textValue = document.getElementById("newId").options[i].text; break; } } window.opener.add("newId", textValue); self.close(); } </script> <BODY> <form id="form1"> <select id="newId"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input type="button" value="submit" onclick="test()"> </form> </BODY> </HTML>
方案2
parent02.html
- <!doctype html public ”-//w3c//dtd html 4.0 transitional//en”>
- <html>
- <head>
- <title>父窗口</title>
- <meta name=”generator” content=”editplus”>
- <meta name=”author” content=”">
- <meta name=”keywords” content=”">
- <meta name=”description” content=”">
- <script type=”text/javascript” src=”/js/jquery/jquery-1.8.1.min.js”></script>
- <script language=”javascript” src=”/js/com.js”></script>
- </head>
- <script type=”text/javascript”>
- $(document).ready(function () {
- $(“#addwindow”).click(function(e) {
- var href = ”/demo/window/son02.html”;
- var textValue = openDialog(href,750, 350, ”yes”, e);
- if (textValue != ”" && textValue != null) {
- add(“newId”,textValue);
- }
- });
- });
- function add(id, textvalue) {
- document.getElementById(id).value = textvalue;
- }
- </script>
- <body>
- <input id=”newId” type=”text”><br>
- <input type=”button” id=”addwindow” value=”addwindow”>
- </body>
- </html>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head> <title>父窗口</title> <meta name="generator" content="editplus"> <meta name="author" content=""> <meta name="keywords" content=""> <meta name="description" content=""> <script type="text/javascript" src="/js/jquery/jquery-1.8.1.min.js"></script> <script language="javascript" src="/js/com.js"></script> </head> <script type="text/javascript"> $(document).ready(function () { $("#addwindow").click(function(e) { var href = "/demo/window/son02.html"; var textValue = openDialog(href,750, 350, "yes", e); if (textValue != "" && textValue != null) { add("newId",textValue); } }); }); function add(id, textvalue) { document.getElementById(id).value = textvalue; } </script> <body> <input id="newId" type="text"><br> <input type="button" id="addwindow" value="addwindow"> </body> </html>
son02.html
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.0 Transitional//EN”>
- <HTML>
- <HEAD>
- <TITLE>子窗口</TITLE>
- <META NAME=”Generator” CONTENT=”EditPlus”>
- <META NAME=”Author” CONTENT=”">
- <META NAME=”Keywords” CONTENT=”">
- <META NAME=”Description” CONTENT=”">
- </HEAD>
- <script type=”text/javascript”>
- function test() {
- var textValue;
- for (var i = 0; i < document.getElementById(“newId”).length; i++) {
- if (document.getElementById(“newId”).options[i].selected) {
- textValue = document.getElementById(“newId”).options[i].text;
- break;
- }
- }
- window.returnValue = textValue;
- window.close();
- }
- </script>
- <BODY>
- <form id=”form1″>
- <select id=”newId”>
- <option value=”1″>1</option>
- <option value=”2″>2</option>
- <option value=”3″>3</option>
- </select>
- <input type=”button” value=”submit” onclick=”test()”>
- </form>
- </BODY>
- </HTML>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>子窗口</TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <script type="text/javascript"> function test() { var textValue; for (var i = 0; i < document.getElementById("newId").length; i++) { if (document.getElementById("newId").options[i].selected) { textValue = document.getElementById("newId").options[i].text; break; } } window.returnValue = textValue; window.close(); } </script> <BODY> <form id="form1"> <select id="newId"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input type="button" value="submit" onclick="test()"> </form> </BODY> </HTML>