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

允我心安 提交于 2019-11-27 21:45:18

问题


Input fields are usually associated to forms, but I would like to use them in a simple Javascript/HTML page. I don't need the form. I see no issue with my HTML page, but is there any danger or bad practice I am not aware of? I just don't want my page to bug down the road.

(Basically, a field in my page can be Javascript enabled or disabled according to values in other fields)


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8568660/is-there-any-danger-to-using-input-fields-outside-without-forms-in-html-javascri

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