How to use jQuery to get the current value of a fi

2020-08-09 06:20发布

From what I've ready you should be able to use the 'value' property of a file input field to get the currently set path of that field. But when I do this:

 $('#fileinput').value()

I get 'undefined'. The ID of the field is set to "fileinput" I'm sure. Does anyone have any thoughts on why this might not be working for me?

And by the way, this works:

var d = document.getElementById('AttachmentFile');
alert(d.value);

So I guess this has something to do with the way jQuery works that I don't fully understand.

Thanks!

8条回答
甜甜的少女心
2楼-- · 2020-08-09 06:31

In Chrome 8 the path is always 'C:\fakepath\' with the correct file name.

查看更多
何必那么认真
3楼-- · 2020-08-09 06:39

Jquery works differently in IE and other browsers. You can access the last file name by using

alert($('input').attr('value'));

In IE the above alert will give the complete path but in other browsers it will give only the file name.

查看更多
Bombasti
4楼-- · 2020-08-09 06:41

Could you also do

$(input[type=file]).val()

查看更多
混吃等死
5楼-- · 2020-08-09 06:47

I've tried this and it works:

 yourelement.next().val();

yourelement could be:

$('#elementIdName').next().val();

good luck!

查看更多
太酷不给撩
6楼-- · 2020-08-09 06:49

its not .val() if you want to get file /home/user/default.png it will get with .val() just default.png

查看更多
冷血范
7楼-- · 2020-08-09 06:55

I don't think there is any real legitimate way to access this via the DOM. It would be a security risk that browsers have of late locked down on to prevent drive-by uploads.

查看更多
登录 后发表回答