--- title: GlobalEventHandlers.onformdata slug: Web/API/GlobalEventHandlers/onformdata translation_of: Web/API/GlobalEventHandlers/onformdata ---
{{domxref("GlobalEventHandlers")}} 混入对象(mixin)的属性onformdata
是用于处理 {{event("formdata")}} 事件的, 它在整个列表展示所构建的表单数据之后被触发。触发会发生在表单发送时,但也可能由对某个{{domxref("FormData.FormData", "FormData()")}} 结构体的调用所触发。onformdata
在 {{domxref("HTMLFormElement")}}上有效。
target.onformdata = functionRef;
functionRef
是一个函数名或者称为 function expression. 此函数接受一个{{domxref("FormDataEvent")}}对象作为其唯一表达式参数。
// grab reference to form const formElem = document.querySelector('form'); // submit handler formElem.addEventListener('submit', (e) => { // on form submission, prevent default e.preventDefault(); // construct a FormData object, which fires the formdata event new FormData(formElem); }); // formdata handler to retrieve data formElem.onformdata = (e) => { console.log('formdata fired'); // Get the form data from the event object let data = e.formData; for (var value of data.values()) { console.log(value); } // submit the data via XHR var request = new XMLHttpRequest(); request.open("POST", "/formHandler"); request.send(data); };
规范 | 状态 | 备注 |
---|---|---|
{{SpecName('HTML WHATWG','webappapis.html#handler-onformdata','onformdata')}} | {{Spec2('HTML WHATWG')}} |
{{Compat("api.GlobalEventHandlers.onformdata")}}
formdata
event