我正在使用 Jquery Form Plugin 通过 Ajax 提交数据,我没有问题。我也成功实现了进度条。
我希望仅当表单具有标签 enctype="multipart/form-data" 时才显示进度条,但是我在读取 AjaxForm 对象中的表单属性时遇到问题:
$('.form_to_main').ajaxForm({
delegation: true,
target: '#main_content',
beforeSend: function(arr, myform, options) {
_is_loading = true;
$('#loading').fadeIn();
//here something like if($(myform).attr("enctype") != '')
$('#loading .progress_bar .bar_container .bar').width('0%')
$('#loading .progress_bar .percent').html('0%');
//else $('#loading .progress_bar).hide();
},
beforeSubmit: function() {
if (_is_loading) return false;
},
uploadProgress: function(event, position, total, percentComplete) {
$('#loading .progress_bar .bar_container .bar').width(percentComplete + '%')
$('#loading .progress_bar .percent').html(percentComplete + '%');
},
error: function(xhr) {
BoxErrorShow('Connection error: ' + xhr.status + ' ' + xhr.statusText);
$('#loading').hide();
_is_loading = false;
},
success: function() {
$('#loading .progress_bar .bar_container .bar').width('100%')
$('#loading .progress_bar .percent').html('100%');
$('#loading').hide();
_is_loading = false;
}
});
});
在 beforeSend: 方法中,我想检查 enctype 属性,但我不清楚如何访问表单对象。
感谢您的支持
更新 - 已解决我在 beforeSend 函数中使用了 beforeSubmit 参数。所以正确的代码如下:
$('.form_to_main').ajaxForm({
delegation: true,
target: '#main_content',
beforeSend: function() {
_is_loading = true;
$('#loading').fadeIn();
$('#loading .progress_bar .bar_container .bar').width('0%')
$('#loading .progress_bar .percent').html('0%');
},
beforeSubmit: function(arr, myform, options) {
if ($(myform).attr("enctype") == 'multipart/form-data') $('#loading .progress_bar').show();
if (_is_loading) return false;
},
uploadProgress: function(event, position, total, percentComplete) {
$('#loading .progress_bar .bar_container .bar').width(percentComplete + '%')
$('#loading .progress_bar .percent').html(percentComplete + '%');
},
error: function(xhr) {
BoxErrorShow('Connection error: ' + xhr.status + ' ' + xhr.statusText);
$('#loading').hide();
_is_loading = false;
},
success: function() {
$('#loading .progress_bar .bar_container .bar').width('100%')
$('#loading .progress_bar .percent').html('100%');
$('#loading .progress_bar').hide();
$('#loading').hide();
_is_loading = false;
}
});
});