weixin_33713350 2017-02-07 08:11 采纳率: 0%
浏览 135

formData和“选择多个” [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/4997252/get-post-from-multiple-checkboxes" dir="ltr">Get $_POST from multiple checkboxes</a>
                            <span class="question-originals-answer-count">
                                (6 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2017-02-07 08:53:49Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I'm trying to submit a dynamically generated form which includes a file upload. For this purpose, I'm getting the form fields via a DB-connection. It is possible for a field to allow multiple selected options. The form can look something like this:

<form>
<input type="text" name="xyz"> </input>
<input type="file" name="xyzfile"> </input>
<select multiple name="...">
<option value="...">1</option>
<option value="...">2</option>
...
<option>x</option>
</select>
<select name="...">
<option value="...">1</option>
<option value="...">2</option>
</select>

Unfortunately, when I use a formData-Object to submit the form, only the last selected option for a given field gets transmitted. The "multiple" attribute is somehow ignored.

This is my code:

var pId = '1';
metaForm = $('#metaForm')[0];
formData = new FormData(metaForm);
formData.append('a','saveInstance');
          formData.append('pId', pId);
$.ajax({
                  type: 'POST',
                  url: 'x.php',
                  contentType: false,
                  processData: false,
                  data: formData,

                  success: function (response) {
...

When I examine the metaForm-Object, all entries that were selected have the attribute selected = true. Is there an option I'm not seeing to get ALL the selected entries for one select-field, when using the formData-constructor? Do I have to do this manually using formData.append()? If so, how do I make sure I do not get any duplicates when using the constructor AND append()?

This is somewhat confusing.

</div>
  • 写回答

1条回答 默认 最新

  • weixin_33736048 2017-02-07 08:47
    关注

    You can serialize your form data before posting like below code:

    var formPostData  = $("#formID").serialize();
    
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划