我的情况是这样的:
Html 代码(这是模态):
<div class="modal-body">
...
<input type="file" id="change-image" name="replace">
...
<button type="button" class="btn btn-success" id="confirm-edit-image">
Save
</button>
</div>
如果我单击输入类型文件,它将运行 javascript 代码:
$('#change-image').change(function (e) {
data = new FormData();
data.append('file', $('#change-image')[0].files[0]);
});
存在可变数据图像
如果我运行这个:
$('#confirm-edit-image').on("click",function(){
// get variable data
});
我想获取可变数据图像
我该怎么做?
data
在这些函数之外声明就足以共享变量本身。这是一个现场片段。
有关 javascript 中变量作用域的更多详细信息,您可以参考这个答案JavaScript 中变量的作用域是什么?.
var data = new FormData();
$('#change-image').change(function (e) {
data.append('file', $('#change-image')[0].files[0]);
});
$('#confirm-edit-image').on("click",function(){
var empty = true;
for (entry of data.entries()) {
console.log(entry);
empty = false;
}
if(empty) { console.log("Please add a file first!\n"); }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="modal-body">
...
<input type="file" id="change-image" name="replace">
...
<button type="button" class="btn btn-success" id="confirm-edit-image">
Save
</button>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句