Javascript 将一个表单的文件输入字段的值复制到另一个表单的输入字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8898077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
copying the value of a form's file input field to another form's input field
提问by pillarOfLight
So I have two forms, both have a file type input field and I tried
所以我有两种形式,都有一个文件类型输入字段,我试过
$('.inputfield1').change(function(){
var file = $(this).val();
$('.inputfield2').val(file);
});
but then it doesn't get copied properly and firebug complains about "Security Error" in the error console
但随后它没有被正确复制,并且萤火虫在错误控制台中抱怨“安全错误”
what did I do wrong and how can I properly copy the value of a file input field
我做错了什么,如何正确复制文件输入字段的值
by the way, the destination form has a target that is set to an iframe (not a different domain)
顺便说一句,目标表单的目标设置为 iframe(不是不同的域)
回答by Kevin B
You can't move the value of one file input to another. Instead, clone the input, place the clone where the original is, and move the original into the hidden form.
您不能将一个文件输入的值移动到另一个。相反,克隆输入,将克隆放置在原始位置,然后将原始移动到隐藏表单中。
$(".inputfield1").change(function(){
var $this = $(this), $clone = $this.clone();
$this.after($clone).appendTo(hiddenform);
});
回答by Adrian Stride
I know it is a late answer, but I had a similar problem that I just figured out today.
我知道这是一个迟到的答案,但我有一个类似的问题,我今天才发现。
What I did was move the File input to the new location after deleting the current one in the other location. By moving the element, everything stayed intact on all my tests.
我所做的是在删除另一个位置的当前文件输入后将文件输入移动到新位置。通过移动元素,在我所有的测试中一切都保持完好。
$('.inputfield1').change(function() {
$('.otherinputfield').remove();
$('#newform').append($(this));
});