admin管理员组文章数量:1202037
The website has 2 input fields, I only select 1 field and the other one is invisible. Now I want to change the .val() of the invisible one to the .val() of the selected one, so both fields upload the same file. How does that work?
If I do this:
$('#input_file').change(function() {
var fileSelect = $(this).val();
$('#hidden_input_file"]').val(fileSelect);
console.log(fileSelect);
});
I get this error: Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
The website has 2 input fields, I only select 1 field and the other one is invisible. Now I want to change the .val() of the invisible one to the .val() of the selected one, so both fields upload the same file. How does that work?
If I do this:
$('#input_file').change(function() {
var fileSelect = $(this).val();
$('#hidden_input_file"]').val(fileSelect);
console.log(fileSelect);
});
I get this error: Uncaught InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
Share Improve this question asked Apr 18, 2015 at 17:53 MaxiMaxi 1231 gold badge2 silver badges8 bronze badges 02 Answers
Reset to default 27Your code is almost correct:
$('#hidden_input_file').val(fileSelect);
This will make jQuery change the value of an input. However, the value of input type="file"
can never* be changed with JavaScript (nor Flash, nor any means programmatically). This is a security feature implemented in all end-user browsers to prevent websites from a trivial mischievery:
$('#hidden_input_file').on('blur', function (e) {
$('#hidden_input_file').val('c:\passwords.txt'); // won't work
});
Enabling the programmatical change of a file URL that is to be uploaded from a user's computer would be a monstrous security issue.
*only applies to JavaScript on the web
You can do it by waiting for the on change event of the original input
$("#Original").on("change", function (event) {
let fileInputElement = document.getElementById('Picture');
fileInputElement.files = event.target.files;
});
本文标签: JavaScriptJQuerySelect Input FILE and also set it to another INPUTStack Overflow
版权声明:本文标题:javascript - jQuery, Select Input FILE and also set it to another INPUT - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738292638a2073237.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论