如何在Jquery中使用动态元素

时间:2018-10-15 14:56:38

标签: javascript jquery html css

我有一个Bootstrap模式。 “模态页脚”区域应该是动态的,因此应该在不同的地方出现不同的“页脚”按钮。示例:

if (type == "CreateAsset") { 
  // 'Next' + 'Submit' + 'Close'  button must be in this area 
} else if (type == "ShowAsset") { 
  // but 'Close' button just to be here. 
}

如果typeCreateAsset,则应显示“下一步”,“提交”和“关闭”按钮,但是如果类型为ShowAsset,则不应显示“下一步”和“提交”。这是为了在不同的地方使用相同的模态。

1 个答案:

答案 0 :(得分:0)

这是您想要的吗?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Dynamic Modal Footer</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"
          integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
            crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49"
            crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy"
            crossorigin="anonymous"></script>
</head>
<body>
<div class="modal" id="my-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Modal title</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                ...
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>
<button id="create-asset-btn">CreateAsset</button>
<button id="show-asset-btn">ShowAsset</button>

<script id="create-asset-footer" type="text/template">
    <button type="button" class="btn btn-primary">Next</button>
    <button type="button" class="btn btn-primary">Submit</button>
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</script>

<script id="show-asset-footer" type="text/template">
    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</script>

<script>
    $(() => {
        let $myModal = $('#my-modal');
        $('#create-asset-btn').on('click', () => {
            $myModal.find('.modal-footer').empty().append($('#create-asset-footer').html()).end().modal('show');
        });
        $('#show-asset-btn').on('click', () => {
            $myModal.find('.modal-footer').empty().append($('#show-asset-footer').html()).end().modal('show');
        });
    });
</script>
</body>
</html>