AJAX Web 应用程序中是否需要表单标签?

2022-01-15 00:00:00 forms xmlhttprequest html ajax

我阅读了一些 AJAX-Form 教程,例如 这个.标签 form 用于 HTML 代码.但是,我认为没有必要.由于我们通过XmlHttpRequest发送HTTP请求,所以发送的数据可以是任何东西,不需要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 应用程序添加 form 标签?

So, is there any reason to have form tag in HTML for AJAX application?

推荐答案

除了已经讨论过的渐进增强(不要让你的网站需要 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.

(当然,您可以尝试在单独的表单字段上捕获按键,但它很繁琐、脆弱,并且永远不会 100% 重现浏览器在构成表单提交方面的本机行为.)

(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.)

相关文章