jQuery 如何克隆值输入文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9658361/
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
How clone value input file
提问by Valentin Hristov
How I can clone value attribute of file input field. Something like this:
如何克隆文件输入字段的值属性。像这样的东西:
<input type="file" id="field1"/>
<input type="file" id="field2"/>
<script>
$('#field2').val($('#field1').val());
</script>
回答by Valentin Hristov
I found solution of this problem:
我找到了这个问题的解决方案:
<input type="file" id="field1"/>
<span id="field2_area"><input type="file" id="field2"/></span>
<script>
$('#field1').change(function(){
var clone = $(this).clone();
clone.attr('id', 'field2');
$('#field2_area').html(clone);
});
</script>
回答by charlietfl
If you are wanting them to stay the same when user interacts with them:
如果您希望它们在用户与它们交互时保持不变:
$(function(){
$('#field1').on('keyup blur', function(){
$('#field2').val($(this).val());
}).blur();
});
Triggering the blur() on page load will do the same as code you already have
在页面加载时触发 blur() 与您已有的代码相同
EDITJust realized these are file fields... browser security limits what you can do with them
编辑刚刚意识到这些是文件字段......浏览器安全限制了你可以用它们做什么