douyin2435 2018-12-16 10:38
浏览 54
已采纳

如何将变量从js传递给模态[关闭]

Hi friends I just wanted to pass a variable to my modal from the script. I am using CodeIgniter as frame work which enables me to generate table with its built-in library. When I press the view button on my table it gets the corresponding id, gets back to controller to grab the table content and returns the table content to the script. Now I have the data in the script called “data[‘viewTable’]” and in my modal id called body have the function call $this->table->generate($tabledata). I just wanted to use the data I have in the script to be in the place of variable $tabledata as it becomes $this->table->generate(data[‘viewTable’]) the question how do I get this data from the script. I have searched for related problems on google but what I got changes the whole content of the modal specified by id. U may suggest me to generate a table inside the script and change the modal by using innerHTML but I tried and still it doesn’t work. Here is partial view of my code

<div id="view_modal" class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="title">View schedules</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="body">
                <?php
              //how do I get the variable in the script to be used in the place of $viewTable
                echo $this->table->generate($viewTable);
                ?>
            </div>
            <div class="modal-footer">
                <button type="button" class="saveview btn btn-primary">Save</button>
                <button type="button" class="closeview btn btn-secondary" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

An on the script I have

$(document).on('click', '.view', function () {
        var id = $(this).data('id');
        var formdata = '&action=viewschedule&ajax=1&id=' + id;
        $.ajax({
            async: false,
            type: "POST",
            data: formdata,
            dataType: "json",
            success: function (data) {
                //Here i got data from the database and I want to pass to modal id 'body'
                $('#view_modal').show();
            }
        });
    });
  • 写回答

1条回答 默认 最新

  • drl2051 2018-12-16 11:53
    关注

    Based on your updated question, you need to just target your #body and updated the html. Here's how you'd do that with the jQuery you're already using

    $('#body').html(data.viewTable);
    

    Solution

    $(document).on('click', '.view', function () {
        // Shortcut for the demo, delete this part
        var data = {viewTable: '<table><tr><td>Hello</td></tr></table>'}
        $('#body').html(data.viewTable);
        $('#view_modal').show();
        
        // Your code, already updated to work.
        var id = $(this).data('id');
        var formdata = '&action=viewschedule&ajax=1&id=' + id;
        $.ajax({
            async: false,
            type: "POST",
            data: formdata,
            dataType: "json",
            success: function (data) {
                $('#body').html(data.viewTable);
                $('#view_modal').show();
            }
        });
    });
    #view_modal {
      display: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <button class="view">View</button>
    <div id="view_modal" class="modal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="title">View schedules</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" id="body"></div>
                <div class="modal-footer">
                    <button type="button" class="saveview btn btn-primary">Save</button>
                    <button type="button" class="closeview btn btn-secondary" data-dismiss="modal">Close</button>
                </div>
            </div>
        </div>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c