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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!