我想在bootstrap模式中实现经典的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>
在模型内容中,有一个用户可以选择其中一个选项。如果单击确定,我想处理新选项。如果不是,则不需要采取任何措施。为此我挂钩了hide.bs.modal事件,但是当点击取消时调用它。
如何区分取消按钮和确定按钮?
答案 0 :(得分:4)
好吧,Bootstrap不支持模态操作按钮的特定模态事件。所以,我相信你必须自己处理这些事件。
$("#myModal").on("click","btn-default", function(){
// code
});
$("#myModal").on("click","btn-primary", function(){
// code
});
答案 1 :(得分:1)
您只需将点击事件添加到“确定”按钮即可?如果您未在活动中添加e.preventDefault()
,则可以正常处理。
首先将确定按钮添加为确定的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>
然后向该按钮添加点击事件:
$("#btn_ok_1").click(function (e) {
var selectedOption = $('select#select1 option:selected').val;
// Do some work with selected item.
})