为什么要使用一个表单标签,当你通过AJAX要提交? [英] Why use a form tag when you're submitting via ajax?

查看:116
本文介绍了为什么要使用一个表单标签,当你通过AJAX要提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哲学问题:

说我有一个web应用程序的需要的JavaScript和现代浏览器,所以渐进式改进是不是一个问题。如果我的形式正在通过JavaScript构建和我的数据更新都被通过AJAX邮电放大器进行;看跌期权,是真的有什么理由来包装一个表单标签我的控制?如果我仍然要使用标签,说语义和结构上的原因,是没有任何理由有动作和方法PARAMS,我要去忽略?那种感觉就像一个套牢了从早期时代的我。

Say I've got a web app that requires javascript and a modern browser, so progressive enhancement is not an issue. If my form is being built via javascript, and my data updates are all being done via ajax POSTs & PUTs, is there really any reason to wrap my controls in a form tag? If I'm still going to use the tag, say for semantic or structural reasons, is there any reason to have action and method params that I'm going to ignore? It kind of feels like a hold-over from an earlier era to me.

推荐答案

还有通过包装投入专门提供一个表单标签内至少有一个重要的用户体验的功能:

There is at least one important user-experience feature provided specifically by wrapping inputs inside a form tag:

回车键提交表单。事实上,在移动Safari浏览器,这是你如何获得转到按钮<一href="http://stackoverflow.com/questions/2990619/mobile-safari-input-field-submit-button/2990714#2990714">to出现在键盘。

The enter key will submit the form. In fact, in Mobile Safari, this is how you get the "Go" button to appear on the keyboard.

如果没有包装的形式投入,没有什么提交。

Without a form wrapping the inputs, there is nothing to submit.

您可以通过一键preSS时提供输入键的行为,但我不知道这是否适用于移动设备。我不知道你怎么想,但我宁愿用浏览器提供的语义工作比有模仿他们的活动。

You can of course provide enter-key behavior through a keypress event, but I don't know about if this works for mobile devices. I don't know about you, but I'd rather work with the semantics provided by the browser than have to imitate them with events.

在你的情况,你只需提供一个的onsubmit 事件处理程序的形式,这将做你的AJAX提交,那么返回false ,取消实际提交。

In your case, you would simply provide an onsubmit event handler for the form, which would do your AJAX submit, then return false, canceling the actual submit.

您可以简单地提供行动=(即自我),而方法不需要&mdash;它默认为 GET

You can simply provide action="" (which means "self"), and method is not required — it defaults to GET.

这篇关于为什么要使用一个表单标签,当你通过AJAX要提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆