jQuery的Ajax调用(在弹出窗口的反应)后不工作 [英] jquery doesn't work after an ajax call (response in a pop up window)

查看:153
本文介绍了jQuery的Ajax调用(在弹出窗口的反应)后不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个jsp页面的表单和一些jQuery code。 jQuery的code完美地工作,但如果我返回该页面在弹出窗口中通过使用Ajax调用,jQuery的code不工作了。

我也尝试使用委派,那就是:

  $('选择[名称= myElementName]')。在(变,函数(){
        //一些code
});

  要么

  $(文件)。在(变,中选择[名称= myElementName]',函数(){
        //一些code
});
 

而不是

  $('选择[名称= myElementName]')。改变(函数(){
             //一些code
    });
 

Ajax调用:

  VAR preVIEW =功能(){
    $阿贾克斯({
       键入:POST,
       网址:myAction.do,
       数据:ID =+本身份识别码,
   成功:函数(响应){
    //一些code
    变种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(响应);

    返回false;
    },
    错误:函数(){
        返回false;
    },
  });
 };
 

修改

在Firefox的26.0和Chrome 32.0.x,我决定用

  x.document.close();
 

  x.document.write(替换);
 

相反,在IE浏览器,所有的.js文件包含的脚本将被忽略(例如jQuery的-UI-1.9.1.js)。

编辑2

我解决了与

 <身体的onload =myload()>
 

和我的JSP我myload()定义,我称之为脚本。

解决方案

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

  $。阿贾克斯({

    ...
    成功:函数(响应){
        //一些code
        变种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(响应);

        //现在,这里将事件处理程序
        $('选择[名称= myElementName]',x.document.body).change(函数(){
                     //一些code
        });
    }

});
 

I've a jsp page with a form and some jquery code. Jquery code works perfectly, but if I return that page in a popup window by using an ajax call, the jquery code doesn't work any more.

I tried also to use delegation, that is:

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

  or

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

instead of

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

Ajax call:

  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;
    },
  });           
 }; 

EDIT

On Firefox 26.0 and Chrome 32.0.x, I resolved by using

 x.document.close();

after

x.document.write(replace);

Instead, on IE, all the .js included scripts are ignored (for example the jquery-ui-1.9.1.js).

EDIT 2

I resolved with

  <body onload="myload()">

and in my jsp I've myload() definition in which I call the scripts.

解决方案

It is because you are creating new DOM structure but it doesn't have the event handlers attached. The easiest way is to run the event handler in the ajax callback:

$.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
        });
    }

}); 

这篇关于jQuery的Ajax调用(在弹出窗口的反应)后不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆