[英]How to show modal of bootstrap 5 after select option and get value?
I have a form that contains a dropdown and some options:我有一个包含下拉列表和一些选项的表单:
<form action="#" class="modal-content">
<select class="form-select" id="value" name="value">
<option value="">Select an option</option>
<option value="1" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="1">Number of Document</option>
<option value="2" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="2">Date</option>
<option value="3" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="3">Seller name</option>
<option value="4" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="4">Create date</option>
</select>
</form>
I also have a Bootstrap modal window:我还有一个 Bootstrap 模态窗口:
<div class="modal fade" id="valueModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="valueModal" aria-hidden="true">
<div class="modal-dialog">
<form action="#" class="modal-content">
<input value="" name="option_id" type="hidden">
<div class="modal-header">
<h5 class="modal-title" id="valueModal">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<!-- Multi Forms -->
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
I would like to show the Bootstrap modal if the value "value" is selected.如果选择值“value”,我想显示 Bootstrap 模式。
I have tried doing this:我试过这样做:
<script>
$("#value").bind("change", function () {
$('#valueModal').modal('show')(this.value === 'option_id');
}).change();
</script>
The modal is opened by default so I know I'm targeting the modal but I would only like to display when the "value" option is selected.默认情况下打开模式,所以我知道我的目标是模式,但我只想在选择“值”选项时显示。
And I have a hidden field in the modal of bootstrap 5我在 bootstrap 5 的模态中有一个隐藏字段
<input value="" name="option_id" type="hidden">
How do get the value after selecting the option value?选择选项值后如何获取值?
Any ideas?有任何想法吗?
I hope this may help我希望这可能有所帮助
$("#value").bind("change", function () { if(this.value >=1) $('#valueModal').modal('show') }).change();
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.1/jquery.min.js"></script> <form action="#" class="modal-content"> <select class="form-select" id="value" name="value"> <option value="">Select an option</option> <option value="1" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="1">Number of Document</option> <option value="2" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="2">Date</option> <option value="3" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="3">Seller name</option> <option value="4" data-bs-toggle="modal" data-bs-target="#valueModal" data-id="4">Create date</option> </select> </form> <div class="modal fade" id="valueModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="valueModal" aria-hidden="true"> <div class="modal-dialog"> <form action="#" class="modal-content"> <input value="" name="option_id" type="hidden"> <div class="modal-header"> <h5 class="modal-title" id="valueModal">Modal title</h5> <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body"> <div class="row"> <!-- Multi Forms --> </div> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </form> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.