简体   繁体   English

jQuery在AJAX加载的DIV中不起作用

[英]jQuery not working in AJAX loaded DIVs

In the HEAD of my document I load jQuery.js and also the blockUI jQuery plugin. 在文档的标题中,我加载了jQuery.js以及blockUI jQuery插件。

In the PHP I then use regular AJAX to load other PHP content into DIVs. 然后,在PHP中,我使用常规AJAX将其他PHP内容加载到DIV中。 In the original PHP jQuery and blockUI plugin work just fine, but in any of the ajax-loaded divs jQuery and blockUI both do absolutely nothing. 在原始的PHP中,jQuery和blockUI插件可以正常工作,但是在任何由ajax加载的div中,jQuery和blockUI都绝对不起作用。 No console error, no warning - nothing. 没有控制台错误,没有警告-什么都没有。

I am a jQuery beginner and none of the other articles I found on this subject were able to put me over the edge of solving this, so I'm helping someone else can. 我是jQuery的初学者,而我在该主题上发现的其他文章都没有让我超越解决此问题的能力,因此,我正在帮助其他人。 In my code below you'll see I took some stabs at live()... 在下面的代码中,您会看到我在live()处刺了一下...

This is at the top of my PHP file that is loaded into the DIV 这是我的PHP文件的顶部,该文件已加载到DIV中

    <script type="text/javascript"> 
    $(document).ready(function() { 

        $('#crazy').live('click',function() { 
            $.blockUI({ message: $('#question'), css: { width: '275px' } }); 
        }); 

        $('#yes').live('click',function() { 
            // update the block message 
            $.blockUI({ message: "<h1>Remote call in progress...</h1>" }); 

            $.ajax({ 
                url: 'wait.php', 
                cache: false, 
                complete: function() { 
                    // unblock when remote call returns 
                    $.unblockUI(); 
                } 
            }); 
        }); 

        $('#no').live('click',function() { 
            $.unblockUI(); 
            return false; 
        }); 

    }); 
</script> 

Here is the HTML from that PHP file (loaded into the DIV): 这是该PHP文件中的HTML(已加载到DIV中):

<input id="crazy" type="submit" value="Show Dialog" /> 

<div id="question" style="display:none; cursor: default"> 
        <h1>Would you like to contine?.</h1> 
        <input type="button" id="yes" value="Yes" /> 
        <input type="button" id="no" value="No" /> 
</div> 

Your document ready function loads when the DOM is loaded, before your AJAX calls complete. 在ADOM调用完成之前,加载DOM时将加载文档就绪功能。 Thus, it only applies the .live() calls to the elements which exist before the AJAX calls. 因此,它仅将.live()调用应用于AJAX调用之前存在的元素。

If you want to apply things to the contents loaded by the AJAX calls, specify a callback function for the AJAX that applies the proper stuff once the loading is complete. 如果要将内容应用于AJAX调用加载的内容,请为AJAX指定一个回调函数,该函数在加载完成后将应用适当的内容。

What is the problem you are getting with the live version? live版本有什么问题? Does it still fail silently? 它仍然默默地失败吗?

Is it possible that the AJAX request in the #yes click event method is failing? #yes点击事件方法中的AJAX请求是否可能失败?

I've taken your code and simplified it a great deal on jsfiddle here , and it seems to be working fine. 我已经在jsfiddle上使用了您的代码并将其简化了很多,而且看起来工作正常。 There isn't an issue with the usage of live and it should fix that event handler for elements being inserted via AJAX. live的使用没有问题,它应该为通过AJAX插入的元素修复该事件处理程序。

Have you tried just throwing alerts in the event methods to see if they get called at all? 您是否尝试过仅在事件方法中引发警报以查看它们是否被调用?

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

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