0

我有一种情况,带有类的锚标记confirm_booking是由 Ajax 生成的。所以我用delegate.

包含此锚点的 div 现在也由 Ajax 生成。
通过 Ajax 加载 div 后,此功能失败。

$('.restro_book_div').delegate('a.confirm_booking', 'click', function (event) {
    event.preventDefault();
    $('ul.time a').removeClass('clicked_confirmed');
    $(this).addClass('clicked_confirmed');
    var $current = $(this);
    var $reserve_restaurant_id = $current.attr('reserve_restaurant_id');
    var $reserve_time = $current.attr('reserve_time');
    var $table_type_id = $current.attr('table_type_id');
    var $reserve_date = $current.attr('reserve_date');
    var $reserve_slot = $current.attr('reserve_slot');
    var $reserve_discount = $current.attr('reserve_discount');
    var $reserve_loyalty_points = $current.attr('reserve_loyalty_points');
    var $reserve_table_type = $current.attr('reserve_table_type');
    //          alert(document.getElementById('standard-dropdown').value);
    //          console.log($current);
    //          console.log($reserve_loyalty_points);
    var $jquery_time = $current.attr('jquery_time');
    var $restro = $current.attr('restro');
    $target = $current;
    var popID = $target.attr('rel'); //Get Popup Name
    var popURL = $target.attr('href'); //Get Popup href to define size
    <?
    php
    if (is_user_logged_in()) { ?>

        $('#' + popID + ' .popup_heading').html('<span>THIS BOOKING IS VALID FOR</span> ' + $reserve_table_type + ' <span>PEOPLE AT</span> ' + $restro + '<br/> <span>ON</span> ' + $reserve_date + ' <span>AT</span> ' + $jquery_time); <? php
    } ?>

    //Pull Query & Variables from href URL
    var query = popURL.split('?');
    var dim = query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({
        'width': Number(popWidth)
    }).prepend('<a style = "display:inline;" href="#" class="close"><img src="<?php echo DIVATEMPLATEPATH."/".IMAGEPATH ?>/close.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top': -popMargTop,
            'margin-left': -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({
        'filter': 'alpha(opacity=80)'
    }).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});
4

1 回答 1

2

然后,您需要定位下一个非动态父级,或者如果没有这样的父级,则定位文档:

$(document).on('click', 'a.confirm_booking', function(event){
    // do stuff
});

并且 on() 将是较新版本的 jQuery 中的首选方法!

于 2013-05-30T11:58:47.790 回答