在IE中迭代FormData

时间:2016-06-21 08:12:47

标签: javascript internet-explorer

我使用FormData将信息发送回服务器。但在某些情况下,我需要在发送数据之前读出数据。

Chrome允许您遍历集合,但IE不提供相同的方法。

以下代码适用于Chrome:

// Create a test FormData object
var formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');

// Display the key/value pairs
for(var pair of formData.entries()) {
   console.log(pair[0]+ ', '+ pair[1]); 
}

JS Fiddle

有谁知道如何在IE中实现相同的结果?感谢。

2 个答案:

答案 0 :(得分:8)

FormData polyfill可行。有关详细信息,请阅读their docs

IE 10,IE 11和Edge

要使其适用于IE 10及更高版本,您只需添加WeakMap polyfill即可。

JSBin demo for IE10 and above.

<script src="https://unpkg.com/weakmap-polyfill/weakmap-polyfill.min.js"></script>
<script src="https://unpkg.com/formdata-polyfill"></script>

<form action="" id="f">
    <input type="text" name="i1" value="v1">
    <input type="text" name="i2" value="v2">
</form>

<script type="text/javascript">
    console.clear();

    // Create a test FormData object
    var formData = new FormData();
    formData.append('key1', 'value1');
    formData.append('key2', 'value2');

    // Display the key/value pairs
    var formDataEntries = formData.entries(), formDataEntry = formDataEntries.next(), pair;
    while (!formDataEntry.done) {
        pair = formDataEntry.value;
        console.log(pair[0] + ', ' + pair[1]);
        formDataEntry = formDataEntries.next();
    }

    // or, if you are really into compact code
    var es, e, pair;
    for (es = formData.entries(); !(e = es.next()).done && (pair = e.value);) {
        console.log(pair[0] + ', ' + pair[1]);
    }

    // testing getting from form
    var myForm = document.getElementById('f');
    for (es = new FormData(myForm).entries(); !(e = es.next()).done && (pair = e.value);) {
        console.log(pair[0] + ', ' + pair[1]);
    }
</script>

以上代码获取最新版本。测试版本:https://unpkg.com/weakmap-polyfill@2.0.0/weakmap-polyfill.min.jshttps://unpkg.com/formdata-polyfill@3.0.9/formdata.min.js


仅限IE11和Edge(如果您不必支持IE10):

如果您只需要IE 11及更高版本,则可以删除WeakMap的polyfill并保留FormData

JSBin demo here

<script src="https://unpkg.com/formdata-polyfill"></script>

<form action="" id="f">
    <input type="text" name="i1" value="v1">
    <input type="text" name="i2" value="v2">
</form>

<script type="text/javascript">
    console.clear();

    // Create a test FormData object
    var formData = new FormData();
    formData.append('key1', 'value1');
    formData.append('key2', 'value2');

    // Display the key/value pairs
    var formDataEntries = formData.entries(), formDataEntry = formDataEntries.next(), pair;
    while (!formDataEntry.done) {
        pair = formDataEntry.value;
        console.log(pair[0] + ', ' + pair[1]);
        formDataEntry = formDataEntries.next();
    }

    // or, if you are really into compact code
    var es, e, pair;
    for (es = formData.entries(); !(e = es.next()).done && (pair = e.value);) {
        console.log(pair[0] + ', ' + pair[1]);
    }

    // testing getting from form element
    const myForm = document.getElementById('f');
    for (es = new FormData(myForm).entries(); !(e = es.next()).done && (pair = e.value);) {
        console.log(pair[0] + ', ' + pair[1]);
    }
</script>

以上代码获取最新版本。测试版本:https://unpkg.com/formdata-polyfill@3.0.9/formdata.min.js

答案 1 :(得分:-1)

var unindexed_array = form.serializeArray();

$.map(unindexed_array, function(n, i){
     console.log(n['name']+ ', '+ n['value']);         
});

此代码在 IE

中给出相同的结果