如何将动态ID从Ajax数据传递到Modal

时间:2019-07-26 03:22:30

标签: javascript ajax bootstrap-modal

我有一个动态ID,该ID是通过ajax POST请求获得的。现在,我想将我的动态ID传递给引导程序模式。有可能吗?

var id = $(this).data('id');

$.ajax({
    type: 'POST',
    url: "{{ url('/pos/createSubCategory') }}",
    data: {id: id},
    success: function (data) {

    var html = '';

    if(!$.trim(data))
    {
        html = '<div>' + '<h3>' + 'Empty!' + '</h3>' +'</div>';
    }
    else
    {
         $.each(data, function () {            
         html += '<div class="card subCategoryClass" data-target="#subcategory_product_modal" data-toggle="modal" aria-hidden="true" data-dismiss="modal" data-id="@this.id">' + '<p>' + '<button>' + this.name + '</button>' + '</p>' +'</div>';

         });
     }


    },
    error: function (data) {
    console.log(data);
    }
});
  

data-id =“ @ this.id”无法正常工作。

1 个答案:

答案 0 :(得分:1)

您必须使用以下解决方案:

由于它是变量,因此您需要使用+运算符对其进行解析。

var data = [{id: "1",name :"rishab"},{id: "2",name :"Ram"}]

var html = "";
$.each(data, function () {            
     html += '<div class="card subCategoryClass" data-target="#subcategory_product_modal" data-toggle="modal" aria-hidden="true" data-dismiss="modal" data-id="'+this.id+'">' + '<p>' + '<button>' + this.name + '</button>' + '</p>' +'</div>';

     });
         
         
console.log(html);
$("#op").append(html)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<div id="op"></div>