简体   繁体   English

将选定的值从子窗口传递到父窗口

[英]Passing selected value from child to parent windows

I have some problem, I have 2 buttons, and they have 2 different values and different ids (let say Button A and Button B), and I want to make that when I click button A the value fill is FORM C OR when I clik button B the value fill is FORM C (form C from the window parent and button A and B from the child parent). 我有一个问题,我有2个按钮,它们有2个不同的值和不同的ID(让我说“按钮A”和“按钮B”),我想确保当我单击按钮A时,值填充为FORM C或单击时按钮B的值填充为FORM C(来自窗口父级的表单C和来自子父级的按钮A和B)。 But in my case if I click button A the passing value to form C is a value from button B. this is the code : 但是在我的情况下,如果我单击按钮A,则向C传递的值是按钮B的值。这是代码:

parent.html parent.html

<form method='post' action='' name='f1'>
<table border=0 cellpadding=0 cellspacing=0 width=550>
<tr>
    <td ><font size="2" face='Verdana'>Your Name</font><input type="text" name="p_name"  size='8'> 
<a href="javascript:void(0);" NAME="My Window Name" title=" My title here " onClick='window.open("child.php","Ratting", "width=550,height=170,left=150,top=200,toolbar=1,status=1,");'>Click here to open the child window</a> 


</td></tr>
</table></form>

child.html child.html

<html>
<head>
<title>(Type a title for your page here)</title>
</head>

<body >

<form name="frm" method="post" action=''>
<table border='0' cellpadding='0' cellspacing='0' width='250'>


 <tr><td align="center">  Your name <input type="button" name="c_name" value="A" onclick="post_value();">


  </td><td align="center">  Your name <input type="button" name="c_name2" value="B" onclick="post_value();">


  </td>

  </tr>

<script langauge="javascript">
function post_value(){
opener.document.f1.p_name.value = document.frm.c_name.value;
opener.document.f1.p_name.value = document.frm.c_name2.value;
 self.close();
}
</script>
</table></form>

In your function you need to detect which button is clicked. 在您的功能中,您需要检测单击了哪个按钮。 So I change the behavior of button by passing a signal to the function: 因此,我通过向函数传递信号来更改按钮的行为:

Setting data on buttons: 在按钮上设置数据:

<input type="button" name="c_name" value="A" onclick="post_value('a');">
<input type="button" name="c_name2" value="B" onclick="post_value('b');">

receive data on function: 接收有关功能的数据:

function post_value(button){
if (button=="a"){
opener.document.f1.p_name.value = document.frm.c_name.value;
}
if (button=="b"){
opener.document.f1.p_name.value = document.frm.c_name2.value;
}
 self.close();
}

As a better solution you can even pass the object (button) itself to the function and extract the value without IF/THEN block and without working with ID: 作为更好的解决方案,您甚至可以将对象(按钮)本身传递给函数,并在没有IF / THEN块且不使用ID的情况下提取值:

Setting data on buttons: 在按钮上设置数据:

<input type="button" name="c_name" value="A" onclick="post_value(this);">
<input type="button" name="c_name2" value="B" onclick="post_value(this);">

receive data on function: 接收功能数据:

function post_value(button){
opener.document.f1.p_name.value = button.value;
 self.close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM