简体   繁体   English

抓取已从AJAX调用“回波”到PHP的div的html

[英]Grabbing html of div that has been 'echoed' from AJAX call to PHP

I am trying to load a dropdown menu from a server and then use jQuery to interact with that dropdown. 我正在尝试从服务器加载下拉菜单,然后使用jQuery与该下拉菜单进行交互。 Everything loads fine, but I can't interact with the menu items as they weren't part of the original HTML that was loaded, they were entered after AJAX got its response. 一切正常,但是我无法与菜单项进行交互,因为它们不是已加载的原始HTML的一部分,它们是在AJAX得到响应后输入的。

Here's the ajax code: 这是ajax代码:

$(document).ready(function(){
    //load dropdown menu using ajax

    $.ajax({ url: "default/CallSupplierMongo",
        context: document.body,
        success: function (res) {
            document.getElementById("dropdownItems").innerHTML = res;
        }
    });

});

The jQuery: jQuery:

$(document).load(function() {
  $('.dropdownItems > div').click(function () {
        supplierCode = $(this).html();

        $('.dropdownItems > div').css({
            'background-color': 'white',
            'color': '#888888'
        });

        $(this).css({
            'background-color': '#888888',
            'color': 'white'
        });

        $('.dropdownItems').slideUp('fast');

        $('.dropdownButton').html(supplierCode);
        $('.dropdownButton').css({
            'border-bottom-right-radius': '5px',
            'border-bottom-left-radius': '5px'
        })
    });

});

And the PHP: 和PHP:

function actionCallSupplierMongo() {
        self::getSuppliers();
}

private static function echoSupplierCodes($supplierCodes) {

    for ($i=0;$i<count($supplierCodes);++$i) {
        echo '<div>'.$supplierCodes[$i].'</div>';
    }

}

To explain the issue further: I want to click on the dropdown items and grab the HTML inside it. 为了进一步解释该问题:我想单击下拉菜单项并获取其中的HTML。 When I click, nothing is registered. 当我单击时,未注册任何内容。 I believe that's because jQuery checks to see if those <div> s exist before they are loaded and thus doesn't apply the .click function to them. 我相信这是因为jQuery在加载<div>之前先检查它们是否存在,因此不对它们应用.click函数。

EDIT I've tried putting the AJAX call in $(document).load() but it still has no effect. 编辑我试过将AJAX调用放在$(document).load()但是它仍然没有效果。

Problem: When you initially load your page, your JS code attaches the events (such as click() ) to the elements that are currently on the DOM, but when you load new elements to the DOM through AJAX, those new elements do not have any events binded to them. 问题:最初加载页面时,JS代码将事件(例如click() )附加到当前在DOM上的元素上,但是当您通过AJAX将新元素加载到DOM上时,这些新元素没有任何绑定到它们的事件。 Thus the events in JS not working. 因此,JS中的事件不起作用。

Solution: You need to delegate the event handler. 解决方案:您需要委派事件处理程序。

Change this: 更改此:

$('.dropdownItems > div').click(function () {

To this: 对此:

$(document).on('click', '.dropdownItems > div', function () {

Source: http://api.jquery.com/delegate/ 资料来源: http//api.jquery.com/delegate/

The problem is that you're attaching click event handlers to items that don't exist yet. 问题是您要将click事件处理程序附加到尚不存在的项目上。

You should use the jQuery's event delegation , eg: 您应该使用jQuery的事件委托 ,例如:

$('.dropdownItems').on('click', '> div', function () {

Note: If, for some reason, you don't want to use event delegation, and prefer to attach the events to the elements themselves, you must put the ready 's code you have into a function, and instead of calling the function when the page is ready, you must call it anytime you modify the collection. 注意:如果由于某种原因,您不想使用事件委托,而是希望将事件附加到元素本身,则必须将已有的ready代码放入函数中,而不是在以下情况下调用函数:该页面已准备就绪,您必须在修改集合时随时调用它。

$('.dropdownItems > div').click(function () {

替换为

$(document).on('click', '.dropdownItems > div', function() {

The answer was to call the function on success of the response from the AJAX call. 答案是在AJAX调用成功响应后调用该函数。

$.ajax({ url: "default/CallSupplierMongo",
    context: document.body,
    success: function (res) {
        document.getElementById("dropdownItems").innerHTML = res;

        loadDropdownFunctions();
    }
});


function loadDropdownFunctions() {

$('.dropdownItems > div').click(function () {
    supplierCode = $(this).html();

    $('.dropdownItems > div').css({
        'background-color': 'white',
        'color': '#888888'
    });

    $(this).css({
        'background-color': '#888888',
        'color': 'white'
    });

    $('.dropdownItems').slideUp('fast');

    $('.dropdownButton').html(supplierCode);
    $('.dropdownButton').css({
        'border-bottom-right-radius': '5px',
        'border-bottom-left-radius': '5px'
    })
});

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

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