Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery没有';ajax调用后无法工作(在弹出窗口中响应)_Javascript_Jquery_Ajax_Popupwindow - Fatal编程技术网

Javascript jquery没有';ajax调用后无法工作(在弹出窗口中响应)

Javascript jquery没有';ajax调用后无法工作(在弹出窗口中响应),javascript,jquery,ajax,popupwindow,Javascript,Jquery,Ajax,Popupwindow,我有一个带有表单和一些jquery代码的jsp页面。Jquery代码工作得很好,但是如果我使用ajax调用在弹出窗口中返回该页面,Jquery代码将不再工作 我还尝试使用授权,即: $('select[name=myElementName]').on("change", function() { // some code }); or $(document).on("change", 'select[name=myElementName]', function()

我有一个带有表单和一些jquery代码的jsp页面。Jquery代码工作得很好,但是如果我使用ajax调用在弹出窗口中返回该页面,Jquery代码将不再工作

我还尝试使用授权,即:

  $('select[name=myElementName]').on("change", function() {
        // some code
});

  or

  $(document).on("change", 'select[name=myElementName]', function() {
        // some code
});
而不是

 $('select[name=myElementName]').change(function() {    
             // some code
    });
Ajax调用:

  var preview = function () {           
    $.ajax({
       type: "POST",
       url: myAction.do,
       data: "id=" + myid,
   success: function (response) {  
    // some code
    var x=window.open('', '_blank', 'titlebar=no,scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,location=0,status=no,left='+left+',top='+top+'');
    x.document.open();
    x.focus();
    x.document.write(response);

    return false;
    },
    error: function () {  
        return false;
    },
  });           
 }; 
编辑

在Firefox26.0和Chrome32.0.x上,我通过使用

 x.document.close();
之后

x.document.write(replace);
相反,在IE上,所有包含的.js脚本都被忽略(例如jquery-ui-1.9.1.js)

编辑2

我决定

  <body onload="myload()">


在我的jsp中有myload()定义,我在其中调用脚本。

这是因为您正在创建新的DOM结构,但它没有附加事件处理程序。最简单的方法是在ajax回调中运行事件处理程序:

$.ajax({

    ...
    success: function (response) {  
        // some code
        var x=window.open('', '_blank', 'titlebar=no,scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,location=0,status=no,left='+left+',top='+top+'');
        x.document.open();
        x.focus();
        x.document.write(response);

        // now, place the event handler here
        $('select[name=myElementName]', x.document.body).change(function() {    
                     // some code
        });
    }

}); 

这是因为您正在创建新的DOM结构,但它没有附加事件处理程序。最简单的方法是在ajax回调中运行事件处理程序:

$.ajax({

    ...
    success: function (response) {  
        // some code
        var x=window.open('', '_blank', 'titlebar=no,scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,location=0,status=no,left='+left+',top='+top+'');
        x.document.open();
        x.focus();
        x.document.write(response);

        // now, place the event handler here
        $('select[name=myElementName]', x.document.body).change(function() {    
                     // some code
        });
    }

}); 

不要使用
文档。write
它会完全覆盖写入时页面上的内容,并导致争用条件(例如,外部脚本可能已经加载,但也可能没有加载,导致
写入和脚本加载的顺序未知)。另外,我相信
documnt.write
是将序列化文本放入文档,而不是DOM对象,因此它可能不会触发事件

相反,您可以打开新窗口,然后直接在那里操作DOM对象(假设它与主页位于同一服务器上):


不要使用
文档。write
它会完全覆盖写入时页面上的内容,并导致争用条件(例如,外部脚本可能已经加载,但也可能没有加载,导致
写入和脚本加载的顺序未知)。另外,我相信
documnt.write
是将序列化文本放入文档,而不是DOM对象,因此它可能不会触发事件

相反,您可以打开新窗口,然后直接在那里操作DOM对象(假设它与主页位于同一服务器上):


我有一个类似的问题,在我的项目之一。我在ajax调用之后编写了一个成功的方法来解决这个问题

            {
             $.ajax({
              type: "POST",
              url: "/abc/",
              data:{<data>},
              async:false,
              dataType:'json',
              success: function(response)                   
              {
                success=1;
                Id=response;
                  return;
              }
        });
        if (success)
        {
                #your code here
                var a='/xyz/?Id='+Id
                window.open(a,'_blank');
                window.location.href='/registration/'
        }
        return false;}
{
$.ajax({
类型:“POST”,
网址:“/abc/”,
数据:{},
async:false,
数据类型:'json',
成功:功能(响应)
{
成功=1;
Id=响应;
回来
}
});
如果(成功)
{
#你的代码在这里
变量a='/xyz/?Id='+Id
窗口。打开(a,“空白”);
window.location.href='/registration/'
}
返回false;}

我在一个项目中遇到了类似的问题。我在ajax调用之后编写了一个成功的方法来解决这个问题

            {
             $.ajax({
              type: "POST",
              url: "/abc/",
              data:{<data>},
              async:false,
              dataType:'json',
              success: function(response)                   
              {
                success=1;
                Id=response;
                  return;
              }
        });
        if (success)
        {
                #your code here
                var a='/xyz/?Id='+Id
                window.open(a,'_blank');
                window.location.href='/registration/'
        }
        return false;}
{
$.ajax({
类型:“POST”,
网址:“/abc/”,
数据:{},
async:false,
数据类型:'json',
成功:功能(响应)
{
成功=1;
Id=响应;
回来
}
});
如果(成功)
{
#你的代码在这里
变量a='/xyz/?Id='+Id
窗口。打开(a,“空白”);
window.location.href='/registration/'
}
返回false;}

不要使用document.write,尝试在隐藏的DIV中获取成功数据(成功函数中到达的记录),并将其克隆到弹出窗口中,该弹出窗口应该可以工作

而不要使用document.write,尝试获取成功数据(成功函数中到达的记录)在一个隐藏的DIV中,将其克隆到弹出窗口中,该弹出窗口应该可以工作

如果您正在调用AJAX服务器例程并将整个响应(不在客户端上处理)放入一个新窗口,为什么不直接用该AJAX例程的URL打开窗口并跳过所有内容:

....
var x=window.open(myAction.do + "?id=" + myid, 
 '_blank', 
 'titlebar=no,scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,location=0,status=no,left='+left+',top='+top+'');
....

这里唯一的区别是,请求是GET而不是POST请求,但数据只是一个id,这可能是可以接受的?

如果调用AJAX服务器例程并将整个响应(不在客户端处理)放入新窗口,为什么不直接用AJAX例程的URL打开窗口并跳过所有内容:

....
var x=window.open(myAction.do + "?id=" + myid, 
 '_blank', 
 'titlebar=no,scrollbars=yes,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,location=0,status=no,left='+left+',top='+top+'');
....

这里唯一的区别是,请求是GET而不是POST请求,但数据只是一个id,这是可以接受的,可能是?

可能重复的
-Infinity
无注释。我无法判断您是否正在尝试运行.on(“更改”…打开的页面上的事件侦听器,或您提出请求的页面上的事件侦听器。请澄清。@Brian Noah在新打开的页面上可能有重复的
-Infinity
无评论。我无法判断您是否正在尝试运行.on(“更改”…打开的页面上的事件侦听器,或您提出请求的页面上的事件侦听器。请澄清。@Brian Noah在新打开的页面上但对于external.js?@cricket什么是external.js?也许您需要更详细地说明问题中的示例?@cricket如果您需要调用一些外部js功能来安装处理程序,然后在ajax
success
callback中执行。在本例中,响应是一个完整的HTML页面。在该页面中,我包含了日期选择器的.js。Firefox和Chrome可以识别它,但不能识别ie。在ie上,我必须将其他脚本放入一个在页面加载时调用的函数中,但我不知道如何处理包含的脚本。@Cricket 1)然后再次从
success
回调调用这些脚本。这是您必须做的。2)我在回答中所写的内容对您演示的代码有帮助。您现在似乎在谈论其他内容。除非您演示