[英]Issue On Getting Uploaded Image Name With Jquery
我正在嘗試獲取這樣的上載圖像名稱
$(function() { $("input[name=file]").on("change", function() { if (this.files && this.files[0]) { var fimeName = $(this).val(files[0].name); console.log(fimeName); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="file" />
但它不起作用,我越來越
未捕獲的ReferenceError:未定義文件
您能告訴我我在做什么嗎? 坦斯克
您需要在click函數中從事件中獲取文件。
$(function() { $("input[name=file]").on("change", function() { if (this.files && this.files[0]) { //what are you trying to do here? var fileName = this.files[0].name; console.log(fileName); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" name="file" />
您忘記了this
:
var fimeName = $(this).val(this.files[0].name);
但是無論如何,有兩件事:您不能在文件的輸入類型上用val()
顯式設置一個值val()
空字符串除外),並且這里將為fimeName
分配this
的jQuery匹配集,而不是任何字符串值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.