如何将值从一个表单的文件上传字段复制到另一个表单的文本字段? [英] How to copy a value from one form's file upload field to another form's text field?

查看:117
本文介绍了如何将值从一个表单的文件上传字段复制到另一个表单的文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一页,上面有两种不同的形式.第一种形式允许用户上传图像文件并通过电子邮件发送,第二种形式根据用户输入生成URL.

I have one page with two different forms on it. The first form allows a user to upload and email an image file, the second form generates a URL based on the user input.

为了将图像名称添加到URL,我需要在第二种形式中有一个字段,该字段从第一种形式中的字段中复制图像名称(我宁愿不必让用户浏览和在一页上选择两次图像).我发现将数据从一个字段跨表格复制到另一个字段的最佳方法是以下jQuery代码: http://jsfiddle. net/nA37d/,但这对我而言不起作用.例如,它在文本字段到文本字段之间的工作都很漂亮,但是在文件字段到文本字段或文件字段到文件字段之间却无效.

In order to add the image name to the URL, I need to have a field in the second form that copies the image name from the field in the first form (I'd rather not have to make the user browse for and select the image twice on one page). The best way I've found to copy data from one field to another across forms is this jQuery code: http://jsfiddle.net/nA37d/, but it doesn't work for my purposes. For example it works from text field to text field beautifully, but it doesn't work from file field to text field, or file field to file field.

是否有一种方法可以在表格1中获取file字段的值并将其复制到表格2中的任何类型的字段中?这是我的基本代码:

Is there a way to grab the value of the file field in form 1 and copy it to any kind of field in form 2? Here is my basic code:

<script type="text/javascript">
$(function(){
    bindGroups();
});

var bindGroups = function() {
    // First copy values
    $("input[name='logotoo']").val($("input[name='logoname']").val());

    // Then bind fields
    $("input[name='logoname']").keyup(function() {
        $("input[name='logotoo']").val($(this).val());
    });
};
</script>


<form action="#..." method="post" enctype="multipart/form-data">
<input type="file" name="logoname" value="1" />
<input type="submit" value="Upload" /></form>


<form name="form2" action="/url/" method="get">
<label>Logo Name</label> <input type="text" name="logotoo" />
<input type="submit" value="Generate URL" /></form>

感谢您提供的任何帮助!

Thanks for any help you can give!

推荐答案

只需对输入类型文件使用change()处理程序,而不要使用键盘输入:

Just use change() handler for input type file, not keyup:

http://jsfiddle.net/nA37d/169/

 $("input[name='a1']").change(function() {
        $("input[name='b1']").val($(this).val());
    });

对于输入文件到输入文件,出于安全原因,我认为这是不可能的.

For input file to input file, i dont think its possible for security reason.

顺便说一句,此代码应重构.

BTW, this code should be refactorized.

这篇关于如何将值从一个表单的文件上传字段复制到另一个表单的文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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