gpt4 book ai didi

javascript - 如何在 Jquery 中使用动态元素

转载 作者:行者123 更新时间:2023-11-28 00:58:24 25 4
gpt4 key购买 nike

我有一个引导模式。 'modal-footer' 区域应该是动态的,所以应该在不同的地方出现不同的“footer”按钮。示例:

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

如果 typeCreateAsset 那么应该显示 Next、Submit 和 Close 按钮​​,但是如果 type 是 ShowAsset 那么 Next 和 Submit 应该不被显示。这是为了在不同的地方使用相同的模态。

最佳答案

这是你想要的吗?

<!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>

关于javascript - 如何在 Jquery 中使用动态元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52819513/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com