2011-05-03 74 views
0

我有打开一个窗口的功能,并且新开​​窗口的值在开瓶器窗口中列出。 该第二窗口 - 具有这样的功能:Javascript开瓶器窗口

function AddOtherRefDoc(name, number) { 
    var remove = "<a href='javascript:void(0);' onclick='removeRefDoctor(this)'>Remove</a>"; 
    var html = "<li><b> Referral Doctor: </b>"+name+"<b>, Referral No: </b>"+number+ " " +remove+" <input type='text' name='ref_docs' value='"+name+"'></input><input type='text' name='ref_nos' value='"+number+"'></input></li>"; 
    opener.jQuery("#r_docs").append(jQuery(html)); 
} 

调用以上所述一个是所述的功能:

function addRefDoc(){ 

    var count = 0; 
    var ref_docarray ; 
    var ref_noarray ; 

    <%for(int i1=0; i1<vec.size(); i1++) { 
     prop = (Properties) vec.get(i1); 
     String ref_no = prop.getProperty("referral_no",""); 
     String ref_name = (prop.getProperty("last_name", "")+ ","+ prop.getProperty("first_name", "")); 
    %> 
    if(document.getElementById("refcheckbox_<%=ref_no%>").checked) { 
     count++; 

     if ((ref_doctor!=null)&&(ref_doctor!="")&&(ref_docno!=null)&&(ref_docno!="")) { 
      ref_docarray = ref_doctor.split(";"); 
      ref_noarray = ref_docno.split(";"); 

      if ((containsElem(ref_docarray,"<%=ref_name%>"))||(containsElem(ref_noarray,<%=ref_no%>))) { 
       alert("Referral doctor " + "<%=ref_name%>" + " already exists"); 
      } else { 
       AddOtherRefDoc("<%=ref_name%>", <%=ref_no%>); 
      } 
     } else { 
      AddOtherRefDoc("<%=ref_name%>", <%=ref_no%>); 
     } 
    } 

    <%} %> 

    self.close(); 
} 

function containsElem(array1,elem) { 
    for (var i=0;i<array1.length;i++) { 
     if(array1[i]==elem){ 
     return true; 
     } else{ 
      return false; 
     } 
    } 
} 

当这个功能被调用时,它应该是携带2个输入元件“ref_docs”和“ref_nos”放入打开此窗口的页面中。但它并没有这样做。它列出了元素,但是当我尝试在第一个窗口的另一个Javascript函数中使用“ref_docs”和“ref_nos”时,我看到“ref_nos”和“ref_docs”是空的。

我在做什么错?

function updateRd(){ 
    var ref_docs = jQuery("#updatedelete").find('input[name="ref_docs"]'); 
    var ref_nos = jQuery("#updatedelete").find('input[name="ref_nos"]');      alert(ref_docs.val() + ref_nos.val()); 
    var rdocs = new Array(); 
    var rnos = new Array(); 
    ref_docs.each(function() { rdocs.push($(this).val()); }); 
    ref_nos.each(function() { rnos.push($(this).val()); });        
    $('#r_doctor').val(rdocs.join(";")); 
$('#r_doctor_ohip').val(rnos.join(";")); } 

- 该函数返回一个错误,说 “ref_docs” 和 “ref_nos” 是不确定的。

回答

1

我认为它试图在其他页面上使用jQuery来查找当前页面上的“#r_docs”。

尝试:

jQuery(opener.document).find("#r_docs").append(html); 

UPDATE:

我创建的index.html:

<!DOCTYPE html> 
<html><head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
    <title> - jsFiddle demo</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script> 
    <script type="text/javascript"> 
    window.jQuery = jQuery; 
    function openChild() 
    { 
     var mychildwin = window.open("child.html"); 
    } 
    </script> 
    </head> 
<body> 

<input type="button" value="click" onclick="openChild();" /> 

<div id="r_docs"> 
Redocs here. 
</div> 
</body> 
</html> 

和child.html:

<!DOCTYPE html> 
<html><head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
    <title> - jsFiddle demo</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.5.2.js"></script> 
    <script type="text/javascript"> 
    function AddOtherRefDoc(name, number) { 
     var remove = "<a href='javascript:void(0);' onclick='removeRefDoctor(this)'>Remove</a>"; 
     var html = "<li><b> Referral Doctor: </b>"+name+"<b>, Referral No: </b>"+number+ " " +remove+" <input type='text' name='ref_docs' value='"+name+"'></input><input type='text' name='ref_nos' value='"+number+"'></input></li>"; 
     jQuery(opener.document).find("#r_docs").append(html); 
    } 
    </script> 
    </head> 
<body> 

<input type="button" value="click" onclick="AddOtherRefDoc('name', 42);"/> 

</body> 
</html> 

更新2:

在您的更新函数document.updatedelete没有属性ref_docs和ref_nos。

尝试:

jQuery("#updatedelete") 
    .find('input[name="ref_docs"], input[name="ref_nos"]') 

如果您的形式是

<form id="updatedelete" ... > 
+0

不,这没没有工作。子弹甚至没有像以前那样显示这个代码。 – Sapphire 2011-05-03 02:03:08

+0

你收到了哪些javascript错误? – 2011-05-03 02:21:56

+0

我解决了它...尝试opener.document而不是opener。 – 2011-05-03 02:27:46

1

你的函数访问DOM元素是不正确。 updatedelete不是document的财产,访问ref_docsref_nos属性也不会自动构建input元素的集合。既然你使用jQuery已经,试试这个:

var ref_docs = $('input[name="ref_docs"]'); 
var ref_nos = $('input[name="ref_nos"]'); 

这将使你的阵列(或至少阵列等)的对象,可以让你访问你的输入:

var rdocs = new Array(); 
var rnos = new Array(); 

ref_docs.each(function() { rdocs.push($(this).val()); }); 
ref_nos.each(function() { rnos.push($(this).val()); }); 

$('#r_doctor').val(rdocs.join(";")); 
$('#r_doctor_ohip').val(rnos.join(";")); 
+0

这也不工作。 ref_docs和ref_nos是空白的。 – Sapphire 2011-05-03 05:17:15

+0

您正在使用错误的文档。请记住,默认情况下,jQuery使用“当前文档”(包含用于加载jQuery的'script'标签的文档)。如果你想在不同的文档中定位标签(如框架或不同的窗口),你必须告诉jQuery。 – 2011-05-03 07:36:53