Why use a form tag when you're submitting via ajax?

前端 未结 7 898
你的背包
你的背包 2020-12-04 20:59

Philosophical question:

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 be

7条回答
  •  失恋的感觉
    2020-12-04 21:32

    Summary: forms OK for MVC, simple web apps, bad for component oriented, rich web apps.

    Reason: forms cannot nest other forms: big limitation for a component-oriented architecture.

    Details: For typical MVC applications, forms are great. In rich, complex web applications using a lot of javascript and AJAX and with a lot of components here and there, I don't like forms. Reason: forms cannot nest another forms. Then if each component renders a form, components cannot nest each other. Too bad. By changing all forms to divs, I can nest them, and whenever I want to grab all parameters in order to pass them to ajax, I just do (with jQuery):

    $("#id_of_my_div").find("[name]").serialize();

    (or some other filtering)

    instead of:

    $("#id_of_my_form").serialize();

    Though, for sentimental and semantic reasons, I keep naming my divs something_form when they are acting as forms.

提交回复
热议问题