2008年2月3日日曜日

JavaScript: window.openで開いた子windowでのフォームの値を親に返す。

Javascriptにおいて、window.openによって、子windowを開き
その子windowでのアクションの結果(例えばフォームでの処理結果)などを親windowに
返すサンプル

parent_window.htmlから子windowを開き、
子window(child_windo.html)内のフォーム(form2)のラジオボタンでチェックした
値を渡すために親windowの関数をwindow.opener.function()を使って実行。ここで
この関数を経由して、親window内のフォーム(form1)の要素に値をセットする。

-------------------- parent_winddow.html start ---------------------------
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=EUC-JP">
<script type="text/javascript">

function select_val(){
var w = window.open("child_window.php", "_blank");
}

function go(val) {
alert(val);
document.form1.bon.value = val;
}

</script>
</head>
<body>
てすと 
<form name="form1" id="form1" >
<input type="text" name="bon" value="init" ><br>
<input type="button" name="" value="open child" onClick="select_val();">
</form>
</body>
</html>
-------------------- parent_winddow.html end ---------------------------




-------------------- chile_window.html start ---------------------------
<html>
<head>
<script type="text/javascript">
function feed(){
for(var i = 0; i < document.form2.aa.length; i++) {
if (document.form2.aa[i].checked == true) {
alert (document.form2.aa[i].value);
window.opener.go(document.form2.aa[i].value);
//document.document.form2.aa[i].value);
}
}
}
</script>
</head>
<body>
<form name="form2" id="form2">
<input type="radio" name="aa" value="bb"> bb<br />
<input type="radio" name="aa" value="cc"> cc<br />
<input type="button" name="" value="戻す" onClick="feed();"> cc<br />
</form>
<body>
</html>
-------------------- chile_window.html end ---------------------------

0 件のコメント: