我正在使用查询文件上传。我在页面上有几个表格,除了一个名为“项目”的隐藏字段外,每个表格都相同。这是处理上传的咖啡脚本:
jQuery ->
$(document).ajaxComplete ->
$('.project_file').fileupload
dataType: "script"
url: ($(this).prev('form').attr("action"))
paramName: 'file_yo'
formData: (form) ->
[{ name: 'authenticity_token', value: $('meta[name="csrf-token"]').attr('content')}, { name: 'project',value: $(this).prev().val()}]
alert($(this).prev().val())
done: (e, data) ->
$('#finish-text').html "Upload finished"
这是其中一种形式的 html。从 from 到 form 的唯一变化是名为“project”的隐藏字段的值。
<form accept-charset="UTF-8" action="/projects/6/project_files" class="edit_project" data-remote="true" id="project_file_form" method="post"><div style="margin:0;padding:0;display:inline"> <input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch"><input name="authenticity_token" type="hidden" value="iIQXYRJfXRFAaSsvSpb4H+HEUMeAI3pubXDRyd+2Ehk="></div>
<input id="project" name="project" type="hidden" value="6">
<input authenticity_token="true" class="field file-field project_file" id="" name="file_yo" title="" type="file" value="">
</form>
问题是 $(this) 是未定义的。当警报出现时,它显示未定义。如何让 $(this) 返回具有类 'project_file' 的特定元素?
更新:
这是咖啡脚本编译的内容:
(function() {
jQuery(function() {
$(document).ajaxComplete(function() {
return $('.project_file').fileupload({
dataType: "script",
url: $(this).prev('form').attr("action"),
paramName: 'file_yo',
formData: function(form) {
return [
{
name: 'authenticity_token',
value: $('meta[name="csrf-token"]').attr('content')
}, {
name: 'project',
value: $(this).prev().val()
}
];
}
}, alert($(this).prev().val()));
});
return {
done: function(e, data) {
return $('#finish-text').html("Upload finished");
}
};
});
}).call(this);