Is there any danger to using input fields outside/without forms in HTML/Javascript pages?

拜拜、爱过 提交于 2019-11-29 01:30:56

You should be fine AFAIK. It's ok in the HTML 4.01 standards anyway

http://www.w3.org/TR/html401/interact/forms.html#form-controls

The elements used to create controls generally appear inside a FORM element, but may also appear outside of a FORM element declaration when they are used to build user interfaces. This is discussed in the section on intrinsic events. Note that controls outside a form cannot be successful controls.

nnnnnn

The only real problem is if you want your page to function for users who have JavaScript disabled - if the inputs are actually for user input then placing them outside a form means that you'd need to use JavaScript (presumably with Ajax) to do anything with the values, whereas form fields can be submitted without JavaScript. If your page isn't intended to be submitted to the server anyway then you're dependent on JavaScript for interaction. If you've taken that into account and it doesn't matter for your scenario then go ahead.

P.S. I should've mentioned that as far as HTML standards go it is perfectly valid to have input elements that aren't in forms.

You can use an HTML validator (here, or on many other sites) to check this sort of thing. If it shows up legal, which I think it should in this case, as Ted pointed out, then you are probably good.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!