在 HTML/Javascript 页面中使用外部/无表单的输入字段是否有任何危险?
输入字段通常与表单相关联,但我想在简单的 Javascript/HTML 页面中使用它们。我不需要表格。我发现我的 HTML 页面没有问题,但是是否存在我不知道的危险或不良做法?我只是不想让我的页面出现问题。
(基本上,我的页面中的字段可以根据其他字段中的值启用或禁用 Javascript)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
唯一真正的问题是,如果您希望页面为禁用 JavaScript 的用户运行 - 如果输入实际上用于用户输入,那么将它们放置在表单之外意味着您需要使用 JavaScript(大概是 Ajax)来执行任何操作与值,而表单字段可以在没有 JavaScript 的情况下提交。如果您的页面无论如何都不打算提交到服务器,那么您就依赖 JavaScript 进行交互。如果您已考虑到这一点并且这对您的场景并不重要,那么请继续。
PS 我应该提到,就 HTML 标准而言,拥有不在表单中的输入元素是完全有效的。
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.
AFAIK你应该没问题。无论如何,在 HTML 4.01 标准中这是可以的
http://www. w3.org/TR/html401/interact/forms.html#form-controls
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
您可以使用 HTML 验证器(此处,或在许多其他网站上)来检查此类内容。如果它显示为合法(我认为在本例中应该如此,正如特德指出的那样),那么您可能就很好。
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.