简体   繁体   English

jQuery脚本仅在IE中打开新窗口后才能工作

[英]Jquery script works only after opening new window in IE

The script below queries a PHP service that returns a JSON response. 下面的脚本查询返回JSON响应的PHP服务。 Buttons are created for each returned record, allowing the user to delete that record from the database. 为每个返回的记录创建按钮,从而允许用户从数据库中删除该记录。 A link is also created below to refresh the data. 在下面还创建了一个链接以刷新数据。

This all works fine in IE, Chrome, Safari...IE (tried 8 & 9), however, is having a strange issue. 在IE,Chrome,Safari ... IE(尝试8和9)中,这一切都可以正常工作,但是出现了一个奇怪的问题。

When the page loads, I am able to refresh the data by clicking the 'refresh' link. 页面加载后,我可以通过单击“刷新”链接来刷新数据。 After this, clicking has no effect UNLESS I open the same page in a different IE window, click the link in the new window, and return to the original window. 此后,单击没有任何作用,除非我在不同的IE窗口中打开同一页面,单击新窗口中的链接,然后返回到原始窗口。 The 'refresh' link then works on the new window ONE time. 然后,“刷新”链接将在新窗口中起作用一次。 It then turns into a vicous cycle. 然后,它变成了一个恶性循环。

Your help is appreciated! 感谢您的帮助!

function getNew(){ 
    $('#new').remove();
    $.getJSON('service.php', function(data) {   
        var items = [];

        items.push('<tr><th>EmplId</th><th>ExternalID</th><th>Name</th></tr>');   

        $.each(data, function(key, val) {

            var indiv = val.toString().split(",");
            items.push('<tr>');
            var id = indiv[0];

            $.each(indiv, function(index, value) {
                items.push('<td align="center" id="' + index + '">' + value + '</td>');
            });

            items.push('<td  class="updateButton" align="center" onclick=\'return update("'+id+'")\'>Update</td>');

        });

        items.push('<tr><td  class="refreshButton" align="center" onclick=\'return getNew();\'>Refresh </td></tr>');

        $('<table/>', {
            'id': 'new',
            html: items.join('')
        }).appendTo('body');
    });
}

function update (emplID){
    $.ajax({
        url: "service.php?emplID="+emplID,
        context: document.body,
        success: function(){
            $('#new').remove(); 
            getNew();
        }
    });
}

I have tried using .live and I get the same results. 我尝试使用.live,但得到的结果相同。

$(".refreshButton").live("click" , function() { 
    getNew(); 
    $.ajax({
        url:     "Service.php?emplID=", 
        context: document.body, 
        success: function(){       
            $('#new').remove();     
            getNew(); } 
    }); 
});

I have disabled the cache still to no avail. 我禁用了缓存仍然无济于事。 The ONLY way to make the link work is to open the same page in a separate window, click the link, and return to the original window. 使链接起作用的唯一方法是在单独的窗口中打开同一页面,单击链接,然后返回到原始窗口。 Is there any solution to this? 有什么解决办法吗?

$(".refreshButton").live( "click" , function() { 
    getNewStudents(); 
    $.ajax({
        url: "studentService.php?emplID=",
        cache: false,
        context: document.body,
        success: function(){

            $('#newStudents').remove();

            getNewStudents();
        }
    });
});

Thanks for your suggestions. 感谢您的建议。 I fixed the tr, but it seems that the solution was to use 我修复了tr,但似乎解决方案是使用

 $.ajaxSetup({ cache: false });

It seems all versions of IE treat ajax calls as normal web requests (cacheable), whereas other browsers consider ajax calls as non-cacheable. 似乎所有版本的IE都将ajax调用视为普通的Web请求(可缓存),而其他浏览器则将ajax调用视为不可缓存。

Learned something new today, thanks guys! 今天学到了新东西,谢谢大家!

Maybe it's because of cache, have you tried to clear your cache and try again? 也许是因为缓存,您是否尝试过清除缓存并重试? this may help http://kb.iu.edu/data/ahic.html#ie8 这可能会有所帮助http://kb.iu.edu/data/ahic.html#ie8

Perhaps IE is choking on the malformed HTML that getNew is generating; IE可能正在getNew生成的格式错误的HTML。 note that in your .each loop, you are not closing the tr element. 请注意,在您的.each循环中,您没有关闭tr元素。 Also, if you're getting a JavaScript error, JS execution may stop, so make sure your browser isn't configured such that it ignores JS errors. 另外,如果遇到JavaScript错误,JS可能会停止执行,因此请确保未对浏览器进行配置,使其忽略JS错误。 Check the error console. 检查错误控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM