开发者

Passing wrong values from Child page to Parent page.

开发者 https://www.devze.com 2023-01-19 13:11 出处:网络
I have Parent page and a Child popup window page (empW.cfm). The Child page has search field,that searches and displays results. I have to select one search result from thre Child page and populate it

I have Parent page and a Child popup window page (empW.cfm). The Child page has search field, that searches and displays results. I have to select one search result from thre Child page and populate it on the Parent page.

I am unable to pass the correct selected value from the Child to Parent page.

Parent page:-开发者_开发知识库

    <script type="text/javascript"  >

    function doSubmit() {
      var Emp = document.getElementById("emp");
      var getName = document.getElementById("getName");
      Emp.value = getName.value;
                }
           </script> 
</head>
<body>
<cfajaximport tags="cfform,cfwindow" scriptsrc="test.js">
<cfform action="Action.cfm" name="formE" id="formE" preserveData="true"  enctype="multipart/form-data" method="post" onsubmit="return validate(document.formE);"  >  
 <table >
    <tbody>    
            <tr><td  > Name*: </td><td><cfinput name="Name" id="Name"  type="text"  ></td></tr>          

             <tr><td > EMP:</td>
<td><input name="searchName" id="emp" onclick="ColdFusion.Window.create('w1','Title','empW.cfm')"></td>

    </tr>  
   </tbody>
 </table>
</cfform>

Child/Window page:-

   <cfform name="formI" id="formI" preserveData="false" method="post">
 <table>
 <tr>SERACH:-  <input name="getName" id="getName" type="text" value="Find emp name" >
<cfif isdefined('form.getName')>
<tr>
<cfloop startrow="1" endrow="qry" query="qry">
   <cfoutput>Selected = #qry.getName# 
 <input name="Add" id="getNm" type="button" value="submit" onclick="document.getElementById('emp').value=document.getElementById('getName').value;">
</cfoutput>
</ cfloop>
tr>

</cfif>
 </table>
</cfform>  

Example:- there are 10 search results on the Child page, and I select the 8th result , still the Parent page is populated by the 1st result only. This is the case when I select every time I select an search result. When I click Submit button on the Child , only the First value of the Search result is being passed.

How to pass the exact selected search result, from the Child to the Parent page.


Two things. You should remove the src from your javascript tag. And move that to a second set of script tags. Your "getName" fields need a unique value. Right now you will end up with multiple getName fields with the same ID. Also, on a side note, you don't need to import cfform if you are using it.


I figured out your problem, Let me explain your problem:

Just understand my simple code:

<html>
    <head>
        <script>
            function transform(){
                var x_elements = document.getElementsByName('x');
                var y_elements = document.getElementsByName('y');
                var i=0;
                for (i=0; i<x_elements.length; i++)
                {
                    y_elements[i].value = x_elements[i].value;
                }
            }
        </script>
    </head>
<body>
    <select name="x" id="x">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
    </select>
    <select name="x" id="x">
        <option value="5">five</option>
        <option value="6">six</option>
        <option value="7">seven</option>
    </select>
    <br/>
    <select name="y" id="y">
        <option value="1">one</option>
        <option value="2">two</option>
        <option value="3">three</option>
    </select>
    <select name="y" id="y">
        <option value="5">five</option>
        <option value="6">six</option>
        <option value="7">seven</option>
    </select>
    <br/>
    <input type="button" value="Check" onclick="javascript:alert(document.getElementById('x').value);" />
    <br/>
    <input type="button" value="Transform" onclick="javascript:transform();" />
</body>
</html>

I've intentionally kept the same name and id to two select tag. While accessing the element by id, you will have access to only first element with same id (e.g. Check button). So you can use documents.getElementsByName().

At last I would still suggest that try to avoid this situation and keep id unique for each element. You can use loop and counter generate unique id. e.g. name1, name2 etc..

For quick and easy javascripting, use jQuery.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号