简体   繁体   English

制作一对多形式的html而不序列化javascript

[英]making a one to many form html that serializes no javascript

I am trying to make a simple form that allows for multiple photos to be added before upload. 我正在尝试制作一个简单的表格,允许在上传之前添加多张照片。 currently I am creating an object manually with javascript 目前,我正在使用javascript手动创建对象

var objArr = [];
for(var i = 0; i < <arrayOfInputs>.length; i++){
    objArr.push({
        img://image file
        caption://caption text
        title://image title
        tags://image tags
    });
}

and then I submit the form. 然后我提交表格。 however is there a way to structure the html markdown so It will have the same effect. 但是,有一种方法可以构造html markdown,因此具有相同的效果。

I tried 我试过了

<form>
    <fieldset name="img[]">
        <input type="file" name="img"/>
        <input type="text" name="caption"/>
        ...
    </fieldset>//repeat for each photo

This does not have the desired effect. 这没有理想的效果。 Is a pure html solution possible or do I need to use javascript to serialize a form manually? 是否可以使用纯HTML解决方案,或者我需要使用JavaScript手动序列化表单?

after serialization object should look something like this:(im using mongodb but that shouldn't effect the structure of the form) 序列化后的对象应如下所示:(即使用mongodb,但这不会影响表单的结构)

{
    'name':'Some Name',
    'description':'Some Description',
    'images':[{
        'img':imgFile,
        'caption':'Some image',
        'title':'Some Title',
        'tags':'Some Tags'
    },{
        'img':otherimgFile,
        'caption':'Some other image',
        'title':'Some other Title',
        'tags':'Some other Tags'
    }]
}

You can add a button that spawns a new fieldset, thus being able to add as many photos as you want before submitting the form. 您可以添加一个生成新字段集的按钮,从而能够在提交表单之前根据需要添加任意数量的照片。

<form>
  <fieldset name="img[]">
    <input type="file" name="img"/>
    <input type="text" name="caption"/>
    <input type="file" name="title"/>
    <input type="text" name="tags"/>
  </fieldset>
  <button id="add-photo-btn">Add another photo</button>
</form>

$('#add-photo-btn').on('click', function() {

  var myForm = $('form');
  var fieldset = '<fieldset name="img[]"><input type="file" name="img"/><input type="text" name="caption"/><input type="file" name="title"/><input type="text" name="tags"/></fieldset>';

  myForm.append(fieldset);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM