【发布时间】:2014-08-09 15:04:07
【问题描述】:
我想阻止用户在上一个 ajax 请求正在进行时停止选择或拖放。
我该怎么做...
这里是代码js代码:
#drag为拖放区的div id
$( '#drag ' ).bind( 'dragover',function(event) {
event.stopPropagation();
event.preventDefault();
});
$( '#drag ' ).bind( 'drop',function(event) {
event.stopPropagation();
event.preventDefault();
if( upfiles == 0 )
{
upfiles = event.originalEvent.dataTransfer.files;
console.dir(upfiles);
upfiles = Array.prototype.slice.call(upfiles, 0);
}
else {
if(confirm( "Drop: Do you want to clear files selected already?" ) == true) {
upfiles = event.originalEvent.dataTransfer.files;
upfiles = Array.prototype.slice.call(upfiles, 0);
$('#fileToUpload').val('');
}
else
return;
}
$( "#fileToUpload" ).trigger( 'change' );
});
点击上传按钮后:
$("#upload_btn").click( function() {
if ( upfiles ) {
$( '#fileToUpload' ).trigger('upload'); // trigger the first 'upload' - custom event.
$(this).prop("disabled", true);
}
});
这里是 ajax 请求:
$( '#container' ).on( 'upload', '#fileToUpload' , function( ) {
if ( typeof upfiles[count] === 'undefined') return false;
var data = new FormData();
var fileIn = $( "#fileToUpload" )[0];
if( !upfiles )
upfiles = fileIn.files;
$(upfiles).each(function(index, file)
{
data.append( 'file'+index, file );
});
var request = $.ajax({
url: 'files.php',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false,
beforeSend: function( ) {
$(".progressbar").show();
},
xhr: function() {
var xhr = $.ajaxSettings.xhr();
if(xhr.upload){
xhr.upload.addEventListener( 'progress', showProgress, false);
}
return xhr;
},
success: function(data){
if( percentComplete <= 100 ) {
$('#pb div').animate({ width: '100%' }, { step: function(now) {
$(this).text( Math.round(now) + '%' );
}, duration: 10});
}
$('#uplcomp').append( data );
}
});
如何在之前的文件上传过程中阻止用户。
更新
ok 到一定程度(但这也不是一个好主意,用户可以从 firebug 中添加 div 并再次发送文件)
我用过
$( document ).ajaxStart(function() {
$( "#total" ).remove();
});
并在 ajax 开始:
$(document).ajaxStop( function( ) {
//how can i add div back say : add <div id='total'></div> after <div id='someid'></div>
});
是否有可能在第一个 ajax 正在进行时停止第二个 ajax 请求?
【问题讨论】:
-
您可以在调用 ajax 时禁用可拖动,并在收到响应时重新启用它:“OK”。
-
是的,我该如何实现...? @EdgarGodyuk
-
如果您使用的是 jQuery Draggable 。我相信你可以在ajax调用之前禁用它 - $(element).draggable('disable') 。并在 ajax 调用返回成功后 - $(element).draggable('enable')
-
我已经清楚地提到了我一直在使用的代码,但我不知道你们为什么要提供 $(element).draggable( 'enable' ) ,有什么可以我这样做,如果这是正确的方法,我如何集成到我发布的上述代码中
-
好吧,我实际上不确定您是如何创建这些可拖动对象的,但是.. 如果您使用 jquery draggable - 在您的 ajax 调用中调用 .draggable( 'disable' ),在发送之前应该可以工作。像这样: beforeSend: function( ) { $(".progressbar").show(); $("your_draggable_element").draggable( 'disable' ) }, ... .... 并且成功:function(data){} 你重新启用它
标签: php jquery ajax file-upload drag-and-drop