我写了一个脚本,打开一个弹出窗口,在找到时搜索用户 用户搜索窗口关闭,然后脚本在父窗口中显示新用户。
基本上我单独尝试了所有脚本,但是当我在windows之间传递时遇到了问题。前面的代码没有通过找到的用户,但是通过一个示例用户同时调试代码。
这是脚本文件:(我稍后会解释)
var orgWindow
var searchWindow
$(document).ready(function(){
$(".add_participants").on("click", function() {
var orgWindow = window.self
var searchWindow = window.open("http://localhost:3000/users/search", "_blank",
"left=200, top=100, height=300, width=300, menubar=yes");
});
});
function printUser(){
var a = orgWindow.document.createElement("a");
a.setAttribute("href","/users/show");
a.setAttribute("id","3");
var aText = orgWindow.document.createTextNode("elad bezalel");
var img = orgWindow.document.createElement("img");
img.setAttribute("alt","El4");
img.setAttribute("src","/uploads/user/image/3/el4.jpg");
img.setAttribute("height","150");
img.setAttribute("width","150");
myDiv = orgWindow.document.getElementById("par");
a.appendChild(img);
var brk = orgWindow.document.createElement("br");
a.appendChild(brk);
a.appendChild(aText);
myDiv.appendChild(a);
}
function add_user(){
user_id = $("#button").data("user_id");
window.close(searchWindow);
window.blur(searchWindow);
window.focus(orgWindow);
orgWindow.document.write(printUser());
orgWindow.close();
}
我正在使用jQuery。当我点击一个按钮时,搜索窗口打开。然后我在搜索窗口中找到一个用户并单击另一个按钮(添加用户),然后关闭搜索窗口并关注父窗口。现在我尝试实现用户图像和名称,但不知何故它不起作用。
我该如何修复我的剧本?
答案 0 :(得分:0)
请注意,您已将orgWindow
和searchWindow
声明为两个不同范围内的变量,全局范围(顶部的两个变量)和点击的本地处理程序。 可以成为您问题的一部分。
答案 1 :(得分:0)
我认为两个单独的窗口上下文混合在一起。
在主页add_user
功能中:
function add_user(user_id){
window.close(searchWindow);
window.blur(searchWindow);
window.focus(orgWindow);
orgWindow.document.write(printUser());
orgWindow.close();
}
在弹出窗口中:
$("#button").click($(window.opener).add_user($(this).data("user_id")));