This question already has answers here:
Event binding on dynamically created elements?

(23个答案)


2年前关闭。




我使用laravel 5.6

我使用波纹管事件并追加div.categoryAjaxId

此代码有效,并附加我的代码。

//--------- get product -------------------------
jQuery('.orderProduct').click(function(e){
    var productId = $(this).attr('id');
    var token = '{{ csrf_token() }}';

    e.preventDefault();

    jQuery.ajax({
        url: "{{ url('order/getCategoryAjax') }}",
        method: 'post',
        data: {
            id: productId,
            _token: token
        },
        success: function(data){
            $('#orderProductSelect').hide();
            $('.categoryAjax').show();
            $.each(data, function(index,category){
                $('.categoryAjax').append('<div class="col-md-4 categoryAjaxId" id="'+category.id+'" style="margin: 10px;cursor: pointer;">' +
                    '<img style="width: 100%;" src="/images/'+category.filename+'">' +
                    '<span>'+category.name+'</span>' +
                    '</div>');
            });
        }});
});


现在我想获取categoryAjaxId ID。发生波纹管事件:

//----- get feature and value of category --------------------
jQuery('.categoryAjaxId').click(function(e){
    var categoryId = $(this).attr('id');
    console.log(categoryId);
    var token = '{{ csrf_token() }}';

    e.preventDefault();

    jQuery.ajax({
        url: "{{ url('order/getFeatureAjax') }}",
        method: 'post',
        data: {
            id: categoryId,
            _token: token
        },
        success: function(data) {
            $('.categoryAjaxId').hide();
            $('.featureAjax').show();
            $('.featureAjax').append('xxxxxxxxxxx');
        }
    });
});


但是此事件不起作用。并且不显示console.log中的任何内容。

谢谢你的帮助

最佳答案

您是否尝试过点击?

//----- get feature and value of category --------------------
jQuery('.categoryAjaxId').on('click',function(e){
var categoryId = $(this).attr('id');
    //console.log(categoryId);
var token = '{{ csrf_token() }}';

e.preventDefault();

jQuery.ajax({
    url: "{{ url('order/getFeatureAjax') }}",
    method: 'post',
    data: {
        id: categoryId,
        _token: token
    },
    success: function(data) {
        $('.categoryAjaxId').hide();
        $('.featureAjax').show();
        $('.featureAjax').append('xxxxxxxxxxx');
    }
});
});

关于jquery - 我用jquery附加数据,但是在laravel中对此数据不起作用任何事件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52512399/

10-14 12:52
查看更多