Yii2将参数从按钮发送到模态ajax

问题描述

我需要将一个参数从按钮放到小部件

data-video =“ fbnSR-KBOiE”

'param'=> 42 像这样'param'=> fbnSR-KBOiE

我该怎么做?

echo ModalAjax::widget([
'id' => 'showVideo','header' => 'Create Company',//'toggleButton' => [
// 'label' => 'New Company',// 'class' => 'btn btn-primary pull-right'
//],'url' => \yii\helpers\Url::toRoute(['youtube/index',**'param' => 42**]),// Ajax view with form to load
'ajaxSubmit' => true,// Submit the contained form as ajax,true by default
// ... any other yii2 bootstrap modal option you need
]);

?>

<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#showVideo" **data-video="fbnSR-KBOiE"** data-backdrop="false">
show video

解决方法

此代码通过ajax通过控制器 youtube / index 将参数从按钮 data-url =“ watch?v = ik7ysDGWbcw” 发送到模态

<span id="play-button" class="fa fa-play-circle play-button" data-toggle="modal" data-target="#youtubeModal" data-url="watch?v=ik7ysDGWbcw" data-backdrop="false">
</span>


<div class="modal fade" id="youtubeModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-body">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
<script>
    $('#youtubeModal').on('show.bs.modal',function (event) {
        let button = $(event.relatedTarget) // Button that triggered the modal
        let youtubeUrl = button.data('url') // Extract info from data-* attributes

        $.ajax({
            url: '<?= \yii\helpers\Url::toRoute(['youtube/index'])?>',type: 'post',data: {youtubeUrl: youtubeUrl},success(response) {
                // Add response in Modal body
                $('.modal-body').html(response);
                //$('#exampleModal').html(response);
                // Display Modal
                //$('#exampleModal').modal('show');
            }
        });
    });
</script>

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...