将输入类型文件值粘贴到输入类型文本 [英] paste input type file value to input type text

查看:72
本文介绍了将输入类型文件值粘贴到输入类型文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的脚本,用于附加一个基于新的基于用户的输入

i have script like this, for append a new elment based user input

var tmp;
$('#add').click(function(){

            var galer=parseInt($('#many').val());

                for(var h=1;h<=tmp;h++){
                    $(".af_"+h).remove();                       
                }

                for(var x=1;x<=galer;x++){
                    $("#kontenPlus").append("<input type='file' id='photo_"+x+"'> <input type='text' id='src_"+x+"'>");

                }

                tmp=galer;                  

       });

for(var u=1;u<=tmp;u++){            
    $('#photo_'+u).change(function(){
            $('#src_'+x).val($(this).val());
    });
}

此html代码:

this html code:

<input type='text' id='many'><div id='add'>Add element</div>
<div id='kontenPlus'></div>

我想问为什么'#photo_ + x',不是放在'#src _'+ x ,函数是否可以循环?

i want to ask why value of '#photo_+x', isn't put in '#src_'+x, is function can be looped ?

LINK: http://jsfiddle.net/rizalfarez/V5AdP/3/

推荐答案

文本框的值未更新的原因是因为它们是动态创建的,所以更改事件不会绑定到他们。

the reason that the value of text boxes does not get updated is because they are dynamically created, so the change event is not bind to them.

我改变了你的代码并优化了它,有点像这样:

I have changed your code and optimized it a little like this:

var tmp;
$('#add').click(function(){
    var galer = parseInt($('#many').val());

    $('.af').remove();                       

    for(var x=1;x<=galer;x++)
        $('#kontenPlus').append('<div class="af"><input type="file" id="photo_'+x+'"><input type="text" id="src_'+x+'"></div>');

    $('[id^="photo_"]').each(function(){
        $(this).change(function(){
            $(this).next().val($(this).val());
        });
    });
});

这篇关于将输入类型文件值粘贴到输入类型文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆