twitter-bootstrap 如果选择了选项,则显示引导模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37643148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show a bootstrap modal if option is selected
提问by Johann
I have a form that contains a dropdown and some options:
我有一个包含下拉列表和一些选项的表单:
<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>
I also have a Bootstrap modal window:
我还有一个 Bootstrap 模式窗口:
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
I would like to show the Bootstrap modal if the value "donations" is selected.
如果选择了值“捐赠”,我想显示 Bootstrap 模式。
I have tried doing this:
我试过这样做:
$("#type").bind("change", function () {
$('#myModal').modal('show')(this.value === 'donations');
}).change();
The modal is opened by default so I know I'm targeting the modal but I would only like to display when the "donations" option is selected.
模态默认打开,所以我知道我的目标是模态,但我只想在选择“捐赠”选项时显示。
Any ideas?
有任何想法吗?
回答by Parvez Rahaman
You try this way..
你试试这个。。
$("#type").on("change", function () {
$modal = $('#myModal');
if($(this).val() === 'donations'){
$modal.modal('show');
}
});
$("#type").on("change", function () {
$modal = $('#myModal');
if($(this).val() === 'donations'){
$modal.modal('show');
}
});
<script src="https://code.jquery.com/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<select name='type' id='type'>
<option value='suggestions' >Suggestions</option>
<option value='inquiries' >Inquiries</option>
<option value='donations' >Donations</option>
</select>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>

