【发布时间】:2015-11-05 08:19:17
【问题描述】:
我有一个关于 javascript 和引导模式的简单问题。我有一个使用文件上传表单的向导,所以我检查文件的名称,如果它不包含一些字符串,我必须显示一个模式,询问你是否要继续。我如何知道从 javascript 或 jquery 中单击了哪个按钮进入模态? 我有这个代码:
if (index==3){
var fileControl = document.getElementById('file');
//Check if the datatable name contains idFLeet and IdCar. REturn -1 if it not contains the string
if (fileControl.files[0].name.indexOf($("#selectedFleet").val()) > -1 && fileControl.files[0].name.indexOf($("#selectedCar").val()) > -1){
//File contains id so you can continue the upload
uploadFunction();
}
else{
$('#warningDatatableModal').modal("show");
//If click on Yes call uploadFunction
//If click on No return false
}
}
HTML 模态代码:
<div class="modal" id="warningDatatableModal" data-backdrop="static" data-keyboard="false">
<div class="modal modal-warning">
<div class="modal-dialog">
<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">Warning</h4>
</div>
<div class="modal-body">
<p>There is a incongruity between file name and fleet and
car previously selected. Are you sure to continue?</p>
</div>
<div class="modal-footer">
<button id="close" type="button" class="btn btn-outline pull-left"
data-dismiss="modal">Close</button>
<button id="continueButton" type="button" class="btn btn-outline">Continue</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
</div>
在 Yeldar Kurmangaliyev 建议后,我更新了我的代码,但向导转到下一个选项卡而不是等待进入实际选项卡
if (index==3){
var fileControl = document.getElementById('file');
//Check if the datatable name contains idFLeet and IdCar. REturn -1 if it not contains the string
if (fileControl.files[0].name.indexOf($("#selectedFleet").val()) > -1 && fileControl.files[0].name.indexOf($("#selectedCar").val()) > -1){
//File contains id so you can continue the upload
uploadFunction();
}
else{
$('#warningDatatableModal').modal("show");
$(".modal-footer > button").click(function() {
clicked = $(this).text();
$("#warningDatatableModal").modal('hide');
});
$("#warningDatatableModal").on('hide.bs.modal', function() {
if (clicked === "Cancel"){
return false;
}else {
uploadFunction();
}
});
}
}
如果我将此代码置于向导工作之外(它不会使用取消按钮关闭模式,因为我使用了向导所需的 return false),但我需要停止向导等待模式结果。
【问题讨论】:
-
显示模态代码
-
你有你的HTML,对吧?所以默认有两个按钮,一个是用于“取消”的(data-target =“close”(类似这样,不记得确切的属性)),用于关闭模式,第二个是“确定”没有任何绑定,您可以在该按钮上嵌套任何您想要的操作,只需使用默认选择器。
$('.modal-footer button#submit').click(function(){ alert(1); }); -
我添加了 HTML 代码,带有 '$('.modal-footer button#submit').click(function(){ alert(1); });'当用户点击按钮时,我不会停止 javascript 的执行
-
为您添加了解决方案
标签: javascript jquery twitter-bootstrap modal-dialog