Iterate through a formData object in Internet Explorer using javascript

Creates a formData object using the form identifier and does the following:

var formDataDetails = new FormData(document.getElementById("form_id")); for (var entry of formDataDetails{ res[entry[0]] = entry[1]; } 

Later I do JSON, strict and doing POST.

But I recently found out that the for..of is not yet supported in Internet Explorer. And I believe that using the for..in loop for..in wrong, as it is used to iterate through enumerated objects (most likely using the properties of the object).

How do I iterate through formData for Internet Explorer?

+7
json javascript internet-explorer form-data
source share
1 answer

I find that from this post IE still does not work reliably for ... looping through FormData objects. So my solution, just avoid FormData when you need to iterate the collection. FormData works great in IE if you just use it to publish form data.

If you need to iterate the form values ​​before submitting them, you can do the same thing as me - just work directly with the form.elements collection.

Something like that:

 export function form2Obj(f) { var elemArray = f.elements; var formObj = {}; for (var k in elemArray) { var input = elemArray[k]; if (!input || !input.name || !input.value) continue; formObj[input.name] = input.value; // etc, need special handling for inputs of type radio // checkbox, textarea, and select most likely } return formObj; 

}

For recording, I use webpack to compile on ES6. When I compile in dev mode, IE can handle the for..of loop. When I compile in production mode, IE is not working.

0
source share

All Articles