如何在模态引导程序中发送参数?

时间:2017-06-07 17:10:29

标签: javascript jquery html ajax modal-dialog

我的观点是这样的:

@foreach($product->photo_list as $i => $photo)
    <a href="javascript:" class="text-danger delete"
       data-toggle="modal"
       data-target="#modal-delete-image"
       data-photo="{{ json_encode($photo) }}" 
       data-product_id="{{ $product->id }}"
    >
        <span class="fa fa-trash"></span>
    </a>
@endforeach

我的模态是这样的:

<bs-modal id="modal-delete-image" v-cloak>
    <h4 slot="title" class="modal-title">...</h4>
    <div slot="body" class="modal-body">
       ...
    </div>
    <div slot="footer" class="modal-footer">
        <button type="button" class="btn btn-danger" id="confirm-delete">
            Delete
        </button>
    </div>
</bs-modal>

我的javascript是这样的:

$('.delete').click(function(){
    var photo = $(this).data('photo');
    var productId = $(this).data('product_id');
    $('#confirm-delete').data('photo', photo, 'productId', productId); //set the data attribute on the modal button
});

$('#confirm-delete').click(function(){
    var photo = $(this).data('photo');
    var productId = $(this).data('product_id');
    console.log(photo)
    console.log(productId)
    $.ajax({
        ...
    });
});

执行代码时,控制台日志的结果为

  

未定义

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

你可以用这个

<button data-photo="photo" data-photo_id="photo-id" type="button" class="btn btn-danger" id="confirm-delete">
    Delete
</button>

$('.delete').on("click",function(){
    var photo = $(this).data('photo');
    var productId = $(this).data('product_id');
    $('#confirm-delete').data('photo', photo);
    $('#confirm-delete').data('productId', productId);
});

$('#confirm-delete').on("click",function(){
    var photo = $(this).data('photo');
    var productId = $(this).data('product_id');
    console.log(photo)
    console.log(productId)
    $.ajax({
        ...
    });
});