在 HTML/Javascript 页面中使用外部/无表单的输入字段是否有任何危险?

发布于 2024-12-21 19:06:18 字数 162 浏览 3 评论 0原文

输入字段通常与表单相关联,但我想在简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯 2024-12-28 19:06:18

唯一真正的问题是,如果您希望页面为禁用 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.

戏蝶舞 2024-12-28 19:06:18

AFAIK你应该没问题。无论如何,在 HTML 4.01 标准中这是可以的

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

用于创建控件的元素通常出现在 FORM 内
元素,但也可能出现在 FORM 元素声明之外
当它们用于构建用户界面时。这在
关于内在事件的部分。请注意,窗体外部的控件不能
成功控制。

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.

捂风挽笑 2024-12-28 19:06:18

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文