我已经尝试了console.log和循环通过它使用for in。

这里是关于FormData的MDN参考。

两种尝试都在这把小提琴上。

var fd = new FormData(),
    key;

// poulate with dummy data
fd.append("key1", "alskdjflasj");
fd.append("key2", "alskdjflasj");

// does not do anything useful
console.log(fd);

// does not do anything useful   
for(key in fd) {
    console.log(key);
}

如何检查表单数据以查看已设置的键。


当前回答

你必须理解FormData::entries()返回一个Iterator实例。

以这个表单为例:

<form name="test" id="form-id">
    <label for="name">Name</label>
    <input name="name" id="name" type="text">
    <label for="pass">Password</label>
    <input name="pass" id="pass" type="text">
</form>

和这个js循环:

<script>
    var it = new FormData( document.getElementById('form-id') ).entries();
    var current = {};
    while ( ! current.done ) {
        current = it.next();
        console.info( current )
    }
</script>

其他回答

你必须理解FormData::entries()返回一个Iterator实例。

以这个表单为例:

<form name="test" id="form-id">
    <label for="name">Name</label>
    <input name="name" id="name" type="text">
    <label for="pass">Password</label>
    <input name="pass" id="pass" type="text">
</form>

和这个js循环:

<script>
    var it = new FormData( document.getElementById('form-id') ).entries();
    var current = {};
    while ( ! current.done ) {
        current = it.next();
        console.info( current )
    }
</script>

MDN建议采用以下形式:

let formData = new FormData();
formData.append('name', 'Alex Johnson')
for(let keyValuePair of formData.entries()){
    console.log(keyValuePair); //has form ['name','Alex Johnson']
}

另外

for (let [key, value] of formData.entries()) {
 console.log(key, ':', value);
}

考虑添加ES+ Polyfills,以防浏览器或环境不支持最新的JavaScript和FormData API。

我希望这能有所帮助。

在angular 7中,我使用下面这行代码在控制台获得了条目。

formData.forEach(entries => console.log(entries));

已经回答了,但是如果你想以一种简单的方式从提交的表单中检索值,你可以使用扩展操作符并创建一个新的Map iterable来获得一个不错的结构。

新地图([…新FormData(形式)])

ES6 +方案:

要查看表单数据的结构:

console.log([...formData])

要查看每个键-值对:

for (let [key, value] of formData.entries()) {
  console.log(`${key}: ${value}`);
}