How clone value input file

2019-05-25 02:49发布

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>

2条回答
淡お忘
2楼-- · 2019-05-25 03:17

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>
查看更多
We Are One
3楼-- · 2019-05-25 03:31

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

EDIT Just realized these are file fields... browser security limits what you can do with them

查看更多
登录 后发表回答