1

我的问题是我是否需要使用表单标签?

如果我不使用 javascript,我会使用表单标签,它会自行提交。我的意思是我没有在客户端编写任何代码。像这样(w3schools.com)

<form name="input" action="html_form_action.asp" method="post">
  Username: <input type="text" name="user">
  <input type="submit" value="Submit">
</form>

使用 Backbone.js,我使用事件:在视图内捕获单击提交按钮,然后保存模型。像这样:

events: {
"click #submit": "SaveData",
},
SaveData: function(){
  /*javascript to save model or post using ajax */
}

显然我需要第一个示例中的表单标签,但如果我使用 ajax/javascript,我是否需要它?

谢谢,

安德鲁

4

3 回答 3

2

My question is do I ever need to use the form tag?

No, you never need to use a form tag. All backbone communication is via ajax.

Yes, as others have mentioned you can optionally construct your form tags, HTML, URLs, and server side code so they work correctly when used via regular form submission and full page loads.

I never use form tags, and here's why:

  • I structure my backbone API URIs to match REST conventions, and my page URIs only sometimes correlate to those
  • I think immediate autosave is a vastly superior interaction pattern, so when you check any checkbox, I send an AJAX PUT and your change is saved immediately without a submit button or full page load. Note that the default behavior of Backbone.Model is to send the entire record, which is not my personal preference, but I'm more of an RPC mindset vs REST.
  • I probably don't want to code my server to work in both single-page fashion and multi-page fashion as doing so is vastly more difficult than the basic single-page style and not worth the effort unless you have extenuating circumstances requiring this.
    • a single page app just needs a basic index.html, a REST API that returns JSON exclusively, and a boatload of browser JS
    • if you want to be able to handle some or all of your operations via full page loads, you have a lot more server side coding to do to render server side HTML that exactly matches what your backbone browser code would do. You need something like airbnb/rendr, and it's non-trivial to put it lightly. Since your example indicates an ASP server, have fun sharing templates between ASP and backbone.
  • if you don't include the form tags at all, you know you don't have to test them. If you include them, you are implying maybe they work, and then probably you should test them.
  • if you don't include the form tags at all, it's one less unexpected set of HTTP requests to worry about on your server. It's clearer about your intent. It's an AJAX/JSON API. That's OK, embrace it.

On the whole "users with javascript disabled" thing - please stop saying this. Can anyone point to a single web application that uses backbone.js when JS is enabled AND can function with JS disabled? I don't think one has ever been written. Plus, it would feel like a time warp to a decade ago. If you are building a backbone app, you are whole-hog into JS. If you want your site to work without JS, don't use backbone, use a traditional multi-page web application framework.

于 2013-07-25T19:35:19.027 回答
1

Ps:我重新阅读了您的问题,并注意到它谈到了 Backbone 特定环境。我不太了解 Backbone,但以下内容应该普遍适用。

就像生活中的大多数事情一样,答案是视情况而定。

考虑一种情况,如果您enter在焦点位于表单的任何输入上时按下键,它将自动提交表单。如果您单击提交类型的按钮/输入,它将提交表单。因此,您可能只监听表单提交事件,阻止默认操作,然后触发您的 ajax 查询。如果您已经有一个表单,那么您可以使用http://api.jquery.com/serialize/来收集 ajax 请求的所有表单字段。

在大多数情况下,我只是让他们在那里。阅读标记时,意图很明确。如果表单有,data-ajax="true"那么我知道它启用了 ajax。从action表单的属性中,我知道了对应的服务器url。我不需要去寻找相应的 js 文件来知道这个表单将被提交到哪里。

于 2013-07-25T19:59:20.250 回答
1

你不需要,但我认为你应该如果它是一个FORM

因为写语义标记会更好。

这是一篇关于语义 html 的文章供您参考: https ://www.adobe.com/devnet/html5/articles/semantic-markup.html

于 2013-07-25T18:49:37.537 回答