Problem Using a Hidden Input value as a parameter in a Javascript Document Ready function
I am integrating an AJAX uploader http://valums.com/ajax-upload/ into my application. By default the script dumps the uploaded file into one prescribed directory. I modified it such that it receives parameters: names of two folders - year and month so it can be e.g. uploads/2010/May/ rather than uploads/ but it still dumps the files in the uploads folder unless I explicitly state the two folder names as strings.
The code below is inside my $(document).ready function.
var uploader = new qq.FileUploader({
element: document.getElementById('uploadfile'),
action: 'ajax/uploader.php',
allowedExtensions: ["xls"],
params: {
f: '2010',//document.getElementById('fileyear').value,
g: 'May'//document.getElementById('filemonth').value
}
});
As seen above, I commented the actual lines and rather used dummy values to check [and it works fine this way].
I have tried using f: $(#fileyear).val(), g: $(#filemonth).val() and it didn't work so I had to default to document.getElementById as above. OUTSIDE the uploader variable, the two values are correct.
A different function stores the date into those hidden inputs, and I changed them to normal text inputs just to be sure that it stored them correctly.
I am guessing the problem here might just be that the uploader is possibly managing to grab the (blank) values before they have been created? Or does this have to do with uploads? Or something 开发者_StackOverflow社区wrong with my document ready? Please I really need help with this - been cracking my head on it all day. Thanks in advance!
The values that you obtain for the document.getElementByID('fileyear').value
etc. will be the values immedaiately after the DOM is loaded, not what you change it to subsequently by typing into the form.
I would make the "f" and "g" options into functions that are called by the uploader to get these values. In this way you will obtain the values at the time of the call, not their initial values (empty?).
var uploader = new qq.FileUploader({
element: document.getElementById('uploadfile'),
action: 'ajax/uploader.php',
allowedExtensions: ["xls"]
});
$('your_button_id').click(function(){
uploader.setParams({
f: document.getElementById('fileyear').value,
g: document.getElementById('filemonth').value
});
});
or
var uploader = new qq.FileUploader({
element: document.getElementById('uploadfile'),
action: 'ajax/uploader.php',
allowedExtensions: ["xls"],
params: {
f: (document.getElementById('fileyear')) ? document.getElementById('fileyear').value : 2010,
g: (document.getElementById('filemonth')) ? document.getElementById('filemonth').value : 'May'
}
});
精彩评论