【问题标题】:Bootstrap : Get Image from modalBootstrap:从模态中获取图像
【发布时间】:2016-10-11 16:52:49
【问题描述】:

我有一个带有一堆图像的模式。图像是可选择的,单击后我将模式按钮文本更改为图像名称。但是我也想抓取图像并显示。我不知道该怎么做,因为我没有使用 javascript 的经验。

html模态代码:

<button type="button" id="modal-btn" class="btn btn-primary" data-toggle="modal"
        data-target="#myModal">
    Click to launch Image Gallery...
</button>
<div id='div_img_name'></div>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
     aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"
                        aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel">Image Gallery</h4>
            </div>
            <div class="modal-body">
                {% for image in images %}
                    <input class="img" type="image" src="{{ image.url }}"
                           height="100" width="130"
                           style="padding-right: 3px;padding-bottom: 3px;">
                {% endfor %}
            </div>
        </div>
    </div>
</div>

用于更改按钮文本的js:

<script type="application/javascript">
    $('#myModal').on('shown.bs.modal', function () {
        $('#myInput').focus()
    })
    $(".img").click(function () {
        $("#modal-btn").text($(this).attr('src'));
        $('#myModal').modal('hide');
        return false;
    })
</script>

如何在div_img_name div 中显示所选图像?

【问题讨论】:

    标签: javascript html django twitter-bootstrap


    【解决方案1】:

    这样的事情怎么样?

    <script type="application/javascript">
        $('#myModal').on('shown.bs.modal', function () {
            $('#myInput').focus()
        })
        $(".img").click(function () {
            var img = $(this).attr('src');
            $("#modal-btn").text(img);
            $('#div_img_name').prepend('<img class="img" type="image" src="' + img + '" height="100" width="130"/>');
            $('#myModal').modal('hide');
            return false;
        })
    </script>
    

    【讨论】:

    • 这行得通。但是如果我再次单击模态按钮并选择另一个图像,旧图像仍然存在并且新图像被添加到它之前(所以现在有 2 个图像)跨度>
    • prepend() 之前使用 - $("#div_img_name").empty(); 解决了 hte 问题
    猜你喜欢
    • 2016-12-05
    • 1970-01-01
    • 2017-08-28
    • 2015-08-15
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    相关资源
    最近更新 更多