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

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

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/https/ask.csdn.net/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();
    
    评论

报告相同问题?