How clone value input file

2019-05-25 02:36发布

问题:

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>

回答1:

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>


回答2:

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