[英]javascript variable scope in this particular instance
In javascript/jQuery, before a file upload plugin is called, a variable is set: 在javascript / jQuery中,在调用文件上传插件之前,先设置了一个变量:
doc_type = "q";
Then, the plugin is initialized. 然后,插件被初始化。 Within the plugin options is:
onSelect:
, which is called when files are selected. 插件选项中包含:
onSelect:
在选择文件时调用。 Code looks like this: 代码如下:
var doc_type = "q";
$(function(){
var project_num = $("#pnum").val();
var uploadObj = $("#fileuploader").uploadFile({
url: "upload_files_processor.php",
method: "POST",
onSelect: function(){
doc_type = "W";
//Or:
//doc_type = $('#hidden_input').val(); <-- What I really need to do
return true;
},
allowedTypes:"pdf,doc,docx,xls,xlsx,ppt,pptx,bmp,jpg,png,zip",
fileName: "myfile",
formData: {"project_num":project_num,"doc_type":doc_type},
multiple: true,
autoSubmit: true,
showStatusAfterSuccess:false,
onSuccess:function(files,data,xhr) {
//Refresh documents table
},
});
}); //END document.ready()
Problem: 问题:
In the upload processor upload_files_processor.php
, the received doc_type
value is: 在上载处理器
upload_files_processor.php
,收到的doc_type
值为:
$doc_type = $_POST["doc_type"]; // q
How can I receive the value W
? 如何获得
W
值?
References: heyageek jquery upload file plugin website -- click on API & Options
tab 参考: heyageek jquery上传文件插件网站 -单击“
API & Options
卡
If you look at the Advanced tab of the api. 如果您查看api的Advanced标签。 There is an option called
dynamicFormData
which is executed and appears to be appended to the formdata right before sending. 有一个名为
dynamicFormData
的选项,该选项将在发送前立即执行并似乎已附加到formdata中。
dynamicFormData: function() {
var data ={"doc_type":doc_type };
return data;
},
This is what you need because doc_type
will be evaluated right before it is sent rather than when it is created. 这就是您需要的,因为
doc_type
将在发送前而不是在创建时进行评估。
IF this works. 如果这可行。 I don't really have a way to test it
我真的没有办法测试
Perhaps you can do it this way: 也许您可以这样:
var formData = {
project_num: null,
doc_type: "q"
};
$(function() {
formData.project_num = $("#pnum").val();
var uploadObj = $("#fileuploader").uploadFile({
url: "upload_files_processor.php",
method: "POST",
onSelect: function(){
formData.doc_type = $('#hidden_input').val();
},
allowedTypes:"pdf,doc,docx,xls,xlsx,ppt,pptx,bmp,jpg,png,zip",
fileName: "myfile",
formData: formData,
multiple: true,
autoSubmit: true,
showStatusAfterSuccess:false,
onSuccess:function(files,data,xhr) {
//Refresh documents table
},
});
});
I am not sure how the plugin works internally. 我不确定该插件如何在内部工作。 If it serializes
formData
into some internal format, you could run into problems. 如果将
formData
序列化为某种内部格式,则可能会遇到问题。 However if it is simply setting the internal formData
attribute to point to a provided object, then this should work. 但是,如果只是将内部
formData
属性设置为指向提供的对象,则应该可以使用。 The reference to formData
remains the same; 对
formData
的引用保持不变; you're just modifying a property of the object that is used. 您只是在修改所使用对象的属性。
In your code, instead of formData: {"project_num":project_num,"doc_type":doc_type},
use this: 在您的代码中,使用以下代码代替
formData: {"project_num":project_num,"doc_type":doc_type},
:
dynamicFormData: function()
{
return {
project_num: $("#pnum").val(),
doc_type: $('#hidden_input').val()
};
},
And remove the lines var doc_type = "q";
并删除行
var doc_type = "q";
and var project_num = $("#pnum").val()
和
var project_num = $("#pnum").val()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.