FormData: keys() method

فهرست فصل‌ها دانلود آرشیوناشر

MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونه‌کد

{{APIRef("XMLHttpRequest API")}} {{AvailableInWorkers}}

The **FormData.keys()** method returns an iterator which iterates through all keys contained in the {{domxref("FormData")}}. The keys are strings.

توجه: Unlike [Map](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) keys, FormData keys are not necessarily unique. A form can contain multiple elements with the same name, so the same key may appear more than once while iterating. To retrieve all values associated with a single key, use the {{domxref("FormData.getAll()", "getAll()")}} method (or {{domxref("FormData.get()", "get()")}} for only the first value).

Syntax

js-nolint
keys()

Parameters

None.

Return value

An [iterator](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols) of {{domxref("FormData")}}'s keys.

Examples

js
const formData = new FormData();
formData.append("key1", "value1");
formData.append("key2", "value2");

// Display the keys
for (const key of formData.keys()) {
  console.log(key);
}

The result is:

plain
key1
key2

Specifications

{{Specifications}}

Browser compatibility

{{Compat}}

See also

  • Using FormData objects
  • {{HTMLElement("Form")}}