2016-07-26 61 views
2

根据我的情况,用户需要提交他/她的个人资料图片,然后获取图片的URL并保存用户配置文件(包括用户名,性别,年龄和配置文件图片的URL)发送到服务器。如何在网页中添加嵌套的HTML表单标签

以下代码不起作用,因为我认为nested-forms是不允许的。那么有人可以帮我解决这个问题吗?

<form role="form" action="/submitTheProfile" method="post" > 
    <input type="text" name="fname" id="f1-first"> 
    <input type="text" name="age" id="f2-first-name"> 
    <input type="text" name="gender" id="f3-first-name"> 

    <form role="form" action="/profilePicUpload" method="post" > 

     <input id="file-0b" class="file" type="file"> 

    </form> 

</form> 
+1

可能重复的[你可以嵌套的HTML表单?](http://stackoverflow.com/questions/379610/can-you-nest-html-forms) –

+0

这可能会帮助你。 http://stackoverflow.com/questions/597596/how-do-you-overcome-the-html-form-nesting-limitation –

+0

答案是:在ajax的帮助下。 – nicael

回答

1

随着世界说你不能使用嵌套form.so JavaScript可以解决您的问题以不同的方式像

HTML

<form role="form" action="/submitTheProfile" method="post" > 
    <input type="text" name="fname" id="f1-first"> 
    <input type="text" name="age" id="f2-first-name"> 
    <input type="text" name="gender" id="f3-first-name"> 

    <input id="file-0b" class="file" type="file"> 

</form> 

的Javascript

document.onload = function(e) { 
    var el = document.getElementById('#file-0b'); el.onchange = function(){ // your code... }; 
}