5 javascript php ajax jquery twitter-bootstrap-3
我正在使用Bootstrap v 3.0.0.我遵循Bootstrap Modal的HTML代码:
<div class="modal fade" id="newModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Submit Form</h4>
</div>
<div class="modal-body" style="max-height: 300px; overflow-y: auto;">
<br/>
<!-- The form is placed inside the body of modal -->
<form id="request_form" method="post" class="form-horizontal" enctype="multipart/form-data" action="">
<div class="form-group">
<label class="col-sm-4 col-sm-offset-1 control-label">Reg ID <span style="color:#FF0000">*</span> :</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="reg_id" id="reg_id"/>
</div>
</div>
<div class="form-group">
<label class="col-sm-4 col-sm-offset-1 control-label">Reg Date<span style="color:#FF0000">*</span> :</label>
<div class="col-sm-5">
<input type="text" class="form-control date_control" id="reg_date" name="reg_date" value="" placeholder="yyyy-mm-dd">
</div>
</div>
<div class="form-group">
<label class="col-sm-4 col-sm-offset-1 control-label">Upload Image<span style="color:#FF0000">*</span> :</label>
<div class="col-sm-5">
<input type="file" name="reg_image" id="reg_image" accept="image/*" capture="camera" />
</div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-5">
<button type="submit" class="btn btn-success" id="btn_receipt_submit">Submit</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
对于上面的bootstrap模式我写了以下AJAX-jQuery代码:
$('#request_form').submit(function(e) {
var form = $(this);
var formdata = false;
var reg_id = $('#reg_id').val();
var reg_date = $('#reg_date').val();
if(window.FormData) {
formdata = new FormData(form[0]);
}
var formAction = form.attr('action');
$.ajax({
url : 'xyz.php',
type : 'POST',
cache : false,
data : formdata ? formdata : form.serialize(),
contentType : false,
processData : false,
beforeSend: function() {
$(".btn").prop('disabled', true); // disable both the buttons on modal
},
success: function(response) {
$(".btn").prop('disabled', false); // enable both the buttons on modal
var responseObject = $.parseJSON(response);
if(responseObject.error_message) {
if ($(".alert-dismissible")[0]) {
$('.alert-dismissible').remove();
}
var htmlString = "<div class='alert alert-danger alert-dismissible' role='alert'><button type='button' class='close' data-dismiss='alert' aria-hidden='true'>×</button>"+responseObject.error_message+"</div>";
$(htmlString).insertBefore('div.modal-body #request_form');
} else {
$('#newModal').modal('hide');
$('#Modal2').modal('show');
}
}
});
e.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)
我想在屏幕中央准确地显示一些适当的加载器图像以及当您的AJAX请求转到PHP文件时它的消息"您的请求正在处理...请等待",它应该正好显示在该文件的中心屏幕直到PHP文件的响应来了.
此外,在此期间,用户不应该关闭模态,如果用户点击模态之外的任何地方,也不应隐藏模态.换句话说,在PHP文件的响应到来之前,用户不应该做任何事情.
我尝试了许多技巧,但我只能禁用表格上出现的两个按钮,直到响应出现.但实际上我想做的远不止这些.
请注意,我希望您的问题应该很接近,因为它“太宽泛”。
您可以尝试禁用模式上的鼠标事件:
$(".modal").css('pointer-events','none');
Run Code Online (Sandbox Code Playgroud)
另请参阅:https ://developer.mozilla.org/en-US/docs/Web/CSS/pointer-events
上面的内容不会禁用按钮(用于打开模式),但您可以保留第一个解决方案。另请注意,您必须将模式的键盘选项设置false
为使用转义键关闭。
z-index
或者,您可以通过代码设置高于模态的覆盖层
CSS:
html, body{
height:100%;
}
#disableall{
position: absolute;
width: 100%;
height:100%;
z-index:1050;
background-color: rgba(255,0,0,0.5);
display:none;
}
Run Code Online (Sandbox Code Playgroud)
html直接在<body>
标签后面:
<div id="disableall"></div>
Run Code Online (Sandbox Code Playgroud)
javascript :
beforeSend: function() {
$("disableall").show();
},
Run Code Online (Sandbox Code Playgroud)