开发者

Should I be using the FORM tag?

I've been using jQuery to build an ajax-heavy site. I typically use jQuery selectors to refer to individual INPUT/BUTTON/TEXTAREA/etc elements I want to interact with (or retrieve values from).

I just realized this morning that I haven't used a FORM tag in ages - I just manipulate everything through ajax requests. My gut tells me this is not good, but on the other hand it's not been an issue (and I don't foresee it becoming one any time soon, all our current needs are met).开发者_如何学运维

One thing to note: I'm not concerned with graceful degradation in absence of JavaScript (it's a corporate LAN; all browser installs are tightly regulated and all users are known to have javascript). With that said, is there some reason I should start using the FORM tag that I'm just not seeing?


Yes, you should still be using form tags. If you want to be really technical, it's probably a good idea to have a non-JavaScript fallback as well. But regardless of that, the main reason for continuing to use <form>, <fieldset>, <legend>, and <label> is for accessibility compliance.

Forms are really the sore spot when it comes to web accessibility. They need to be conducted perfectly for smooth operation with screen readers or other assistive technologies. In fact, according to ADA and Section 508 laws and regulations, all those tags are required when making forms online. Many people would argue that federal regulations only apply to government and state websites, but you can tell that to Target, who was sued over web accessibility issues.

Really, it's a good idea to just obey the rules. And, personally, making the form submit with or without AJAX is pretty easy. For example, I currently have several forms that are imperative that they can be used with out without JavaScript. All the forms are validated and submit via JavaScript (Yes, they are secondarily validated through the PHP script too). The script the JavaScript submits to is the same page the regular form submits to via cURL. One process page for two different methods with the same results (delivered in JSON and decoded via jQuery or PHP's json_decode), just the JavaScript version is cleaner, elegant, and more seamless.

In conclusion, I feel your users would benefit more from including the proper tags then excluding them, even if the benefits don't feel obvious to you or them.


Well, form elements are supposed to go in... forms, right? The HTML5 spec (and pretty much any HTML spec) doesn't mandate having <input> elements et al within <form> elements, but you don't really have a form in your page unless you state in the markup that there is, in fact, a form.

I reckon you'd even have an easier time dealing with form submissions if jQuery knows which form is being submitted as well, among other things.

Also, you have users with JavaScript disabled to think about. Even if you don't care about those users, it's still important that your web app be able to handle "traditional" form submissions if they aren't otherwise being handled by jQuery/Ajax and such.


Yes.

When JavaScript is disabled, using <form> you can still submit the data to server. Considering the valid HTML rules, a FORM tag is required around the form elements. And, accessibility tools such as screen readers could use FORM tags to give more information to the users.

So, you should use FORM tag. :)


A good reason to use them is that you can use http://api.jquery.com/serialize/ and http://api.jquery.com/serializeArray/ on <form>s easily and use HTTP posts more easily (as you have mentioned).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜