我想清除無效影像格式的影像。我已經嘗試了多次,但沒有奏效。我怎樣才能實作它。使用 jQuery 3.6.0
$(':file').change(function(){
var fileArray = this.files;
var validImageTypes = ["image/gif", "image/jpeg", "image/png"];
$.each(fileArray,function(i,v){
type = v.type;
if ($.inArray(type, validImageTypes) < 0) {
alert('Only jpg, jpeg and png is allowed.');
// here i want to clear the image which is selected
};
})
});
我試過使用$(this).val("")但不作業。請幫忙。
uj5u.com熱心網友回復:
this指向每個呼叫的函式。
使用箭頭函式自動系結到父背景關系(檔案元素)。
$(':file').change(function() {
var fileArray = this.files;
var validImageTypes = ["image/gif", "image/jpeg", "image/png"];
$.each(fileArray, (i, v) => {
type = v.type;
if ($.inArray(type, validImageTypes) < 0) {
alert('Only jpg, jpeg and png is allowed.');
// here i want to clear the image which is selected
$(this).val("");
};
})});
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/489719.html
標籤:javascript jQuery
上一篇:如何在TypeScript(JavaScript)中添加和減去復選框值
下一篇:如何洗掉jquery中的附加元素
