[英]OK and Cancel in a bootstrap modal
I would like to implement a classic OK and Cancel concept in a bootstrap modal.我想在引导模式中实现经典的 OK 和 Cancel 概念。
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
user can select an option here from a list
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary">OK</button>
</div>
</div>
In the model content, there is a and the user can select one of the options.在模型内容中,有一个,用户可以选择其中一个选项。 If OK is clicked, I would like to handle the new option.
如果单击确定,我想处理新选项。 If not, no action is required.
如果不是,则不需要任何操作。 For that I hook on the hide.bs.modal event, but that is called when Cancel is clicked.
为此,我挂上了 hide.bs.modal 事件,但在单击 Cancel 时会调用该事件。
How can I differentiate between Cancel and OK buttons?如何区分“取消”和“确定”按钮?
Well, Bootstrap does not support specific modal events for the modal actions buttons.好吧,Bootstrap 不支持模态操作按钮的特定模态事件。 So, I believe you will have to handle the events yourself like so.
所以,我相信你必须像这样自己处理这些事件。
$("#myModal").on("click",".btn-default", function(){
// code
});
$("#myModal").on("click",".btn-primary", function(){
// code
});
You can simply add an click event to OK button?您可以简单地向“确定”按钮添加单击事件? If you do not add
e.preventDefault()
to your event you can process normally.如果您不将
e.preventDefault()
添加到您的事件中,您可以正常处理。
First add your OK button a definitive ID or class name:首先为你的 OK 按钮添加一个明确的 ID 或类名:
<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">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
user can select an option here from a list
<select name="select" id="select1">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
</select>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" id="btn_ok_1" class="btn btn-primary">OK</button>
</div>
</div>
</div>
Then add a click event to that button:然后向该按钮添加一个点击事件:
$("#btn_ok_1").click(function (e) {
var selectedOption = $('select#select1 option:selected').val;
// Do some work with selected item.
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.