建站教学网,让你建站更便捷和轻松!QQ交流群:78085540
您当前的位置:首页 > 网页制作 > Javascript/Ajax

输入框点击弹出选择值后返回该页面的js代码

来源:建站教学网  时间:2014-06-26 22:25:20
父窗口代码:(命名随意)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
var child;
function onClick () {
window.open('childwindow.html');
}
function setData (id, name) {
document.getElementById('id').value = id;
document.getElementById('name').value = name;
}
</script>
</head>

<body>
<p>id:<input type="text" id="id"/></p>
<p>name:<input type="text" id="name" /></p>
<p><input type="button" value="弹出" onclick="onClick();" /></p>
</body>
</html>

子窗口代码:(childwindow.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<table border="1">
<tr onclick="opener.setData(this.childNodes[0].innerHTML, this.childNodes[1].innerHTML);window.close();"><th>id</th><th>name</th></tr>
<tr onclick="opener.setData(this.childNodes[0].innerHTML, this.childNodes[1].innerHTML);window.close();"><td>1</td><td>Tony</td></tr>
<tr onclick="opener.setData(this.childNodes[0].innerHTML, this.childNodes[1].innerHTML);window.close();"><td>2</td><td>Lily</td></tr>
<tr onclick="opener.setData(this.childNodes[0].innerHTML, this.childNodes[1].innerHTML);window.close();"><td>3</td><td>John</td></tr>
</table>
</body>
</html>

// 多浏览器下测试通过

 

 

父窗口代码(parent.html)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
</head>
<body>
<input type="text" name="data_key" id="data_key" />
<input type="text" name="data_value" id="data_value" />
<input type="button" id="open_child_window" value="list_datas"/>
<script type="text/javascript">
window.onload = function(){
//取得打开子窗口的按钮
var openChildBtn = document.getElementById("open_child_window");
//打开新窗口函数
function openNewWindow(url){
window.open(url);
}
//调用打开新窗口函数,指定新窗口地址
function openChildWindow(){
openNewWindow("child.html");
}
//为按钮绑定事件
if (document.all) {
openChildBtn.attachEvent("onclick",openChildWindow);//IE
}else{
openChildBtn.addEventListener("click",openChildWindow);//非IE
}
}
</script>
</body>
</html>

子窗口代码(child.html)这个必须按这个命名,如果要改,需要把父窗口代码里面的child.html也同步改掉

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Page</title>
<style type="text/css">
body{font-size:12px}
.data_list{background:#ddf}
.data_list td{background:white}
</style>
</head>
<body>
<table cellspacing="1" cellpadding="3" border="0" class="data_list">
<tr>
<td id="data_key_1">data_key_1</td>
<td id="data_value_1">data_value_1</td>
<td><input type="button" id="data_submitor_1" value="submit"/></td>
</tr>
<tr>
<td id="data_key_2">data_key_2</td>
<td id="data_value_2">data_value_2</td>
<td><input type="button" id="data_submitor_2" value="submit"/></td>
</tr>
<tr>
<td id="data_key_3">data_key_3</td>
<td id="data_value_3">data_value_3</td>
<td><input type="button" id="data_submitor_3" value="submit"/></td>
</tr>
<tr>
<td id="data_key_4">data_key_4</td>
<td id="data_value_4">data_value_4</td>
<td><input type="button" id="data_submitor_4" value="submit"/></td>
</tr>
</table>
<script type="text/javascript">
//向父窗口发送数据
function SendDataToParent(key,value){
if (window.opener && window.opener.document.getElementById("data_key")&& window.opener.document.getElementById("data_value")) {
window.opener.document.getElementById("data_key").value = key;
window.opener.document.getElementById("data_value").value = value;
}
}
window.onload = function(){
//如果父窗口已经被关闭或者不存在,直接返回
if (!window.opener) return ;
//遍历所有的数据
for(var i=1;i<5;++i){
//取得提交按钮
var btn = document.getElementById("data_submitor_" + i.toString());
//如果按钮有效
if (btn) {
//为按钮绑定事件
if(document.all){//IE
//attachEvent响应函数中this指针是指向window而不是指向事件响应对象的,所以需要一个函数来实现对象传递
function GetEventHandle(tag) {
return function () {
var temp = tag.id.split("_");
var index = temp[temp.length-1];
SendDataToParent(document.getElementById("data_key_"+index).innerHTML,document.getElementById("data_value_"+index).innerHTML);
}
}
btn.attachEvent("onclick",GetEventHandle(btn));
}else{//非IE
btn.addEventListener("click",function(){
var temp = this.id.split("_");
var index = temp[temp.length-1];
SendDataToParent(document.getElementById("data_key_"+index).innerHTML,document.getElementById("data_value_"+index).innerHTML);
});
}
}
}
}
</script>
</body>
</html>
点击触发事件
父窗口ID为XX的对象赋值为name是YY的值
window.parent.document.getElementById('xx').value = document.getElementsByName('YY').value;

责任编辑:admin

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
CopyRight 2008-2009, JZREAD.COM, Inc. All Rights Reserved 联系QQ:568245346
皖ICP备09015033号