var getFileName = null
$('#fileInput').change(function() {
var props = $('#fileInput').prop('files'),
file = props[0]
getFileName = "" + file.name
console.log("inside function: " + getFileName)
})
// selected file
console.log("outside function: " + getFileName);
变量在函数内正确更改,但我无法在函数外部获取更改的值。我想我忽略了一些东西,但目前我无法修复它。😅
就像注释中提到的,这些行没有按照回调的顺序执行。
你可能应该这样做
var getFileName = null; // global variable
$('#fileInput').change(function() {
var props = $('#fileInput').prop('files'),
file = props[0],
getFileName = "" + file.name
;
console.log("inside function: " + getFileName); // log in callback
checkOutside();
})
function checkOutside(){
// output global variable after its changed in callback of "change"-event
console.log("outside function: " + getFileName);
}