問題描述
我閱讀了一些 AJAX-Form 教程,例如 這個.標(biāo)簽 form 用于 HTML 代碼.但是,我認(rèn)為沒有必要.由于我們通過XmlHttpRequest發(fā)送HTTP請求,所以發(fā)送的數(shù)據(jù)可以是任何東西,不需要form中的input.
I read some AJAX-Form tutorial like this. The tag form is used in HTML code. However, I believed that it is not necessary. Since we send HTTP request through XmlHttpRequest, the sent data can be anything, not necessary input in form.
那么,有沒有理由在 HTML 中為 AJAX 應(yīng)用程序添加 form 標(biāo)簽?
So, is there any reason to have form tag in HTML for AJAX application?
推薦答案
除了已經(jīng)討論過的漸進(jìn)增強(qiáng)(不要讓你的網(wǎng)站需要 JavaScript,直到它真的必須),一個 <form>
和 onsubmit
是可靠捕獲 Enter 按鍵提交所必需的.
Apart from progressive enhancement as already discussed (don't make your site require JavaScript until it really has to), a <form>
with onsubmit
would be necessary to reliably catch an Enter keypress submission.
(當(dāng)然,您可以嘗試在單獨(dú)的表單字段上捕獲按鍵,但它很繁瑣、脆弱,并且永遠(yuǎn)不會 100% 重現(xiàn)瀏覽器在構(gòu)成表單提交方面的本機(jī)行為.)
(Sure, you can try trapping keypresses on separate form fields, but it's fiddly, fragile and will never 100% reproduce the browser's native behaviour over what constitutes a form submission.)
這篇關(guān)于AJAX Web 應(yīng)用程序中是否需要表單標(biāo)簽?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!