使用 ids 而不是名称属性来标识表单中的字段有什么隐藏的原因吗?

发布于 2024-12-08 01:55:25 字数 456 浏览 0 评论 0原文

我正在开发一个大型 Web 应用程序项目,以前的设计者喜欢使用 ids 作为句柄来形成字段而不是名称属性。

我想这样做的一个优点是通过 Javascript 查找该字段通过 ids 更快。

然而,我现在遇到的一个大问题是 ids 具有全局范围。我想将大量数据库列名重构为更标准的命名方案,其中不包含任何列名前缀来标识该列属于哪个表。这会在使用 ids 的表单中引起问题,因为字段 ids 直接对应于列名。诸如“zon_name”和“pro_name”之类的列名称现在都只是“name”。这将导致 html 中的 id 不唯一。

因此,在冗长的序言之后,这是我的问题...

在我尝试通过更改所有表单以使用名称属性而不是 id 来解决此范围界定问题之前,是否还有其他原因我没有考虑到原始开发人员可能有除了查找速度快之外,还需要使用 ids 吗?

我知道这是一篇很长的文章,所以我感谢任何有勇气阅读并给出良好答案的人。谢谢!

I am working on a large web application project and the previous designer favored the use of ids as handles to form fields over name attributes.

I suppose one advantage of this is that the lookup of that field via Javascript is faster through ids.

A big problem I'm now running into, however, is that ids have global scope. I want to refactor a large set of database column names to a more standard naming scheme, which doesn't include any column name prefix to identify which table the column belongs to. This is going to cause problems in those forms that use ids, since the field ids correspond directly to the column names. Column names which were things like "zon_name" and "pro_name" are now going to both be just "name". This will cause non-unique ids in the html.

So, after that long preamble, here's my question...

Before I try to address this scoping issue by changing all the forms to use name attributes instead of ids, are there any other reasons I'm not considering that the original developer may have had for using ids besides the speediness of their lookup?

I know this is a long one so I appreciate anyone who is brave enough to read through and give a good answer. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小傻瓜 2024-12-15 01:55:25

name 和 id 执行不同的操作,虽然存在一些重叠,但对于它们执行的最重要的操作来说,它们不可互换。

使用名称

  • 确定将表单提交到服务器时为数据指定的键
  • 创建单选组
  • 当您需要一次引用多个表单控件(以及添加一个表单控件时)从 JS/CSS class 或者使用元素类型都不太合适)

使用 id

  • 在控件的 for 属性中
  • 来自 JS/CSS 时您需要引用特定的输入

我认为这样做的一个优点是通过 Javascript 查找该字段通过 ids 速度更快。

不显着(特别是当名称是唯一的时)。

听起来原始设计者并没有遵循标准约定,而是想出了一些高度依赖 JavaScript 的东西。

Name and id do different things and, while there is some overlap, they are not interchangeable for the most important things they do.

Use name

  • To determine what key will be given to the data when the form is submitted to the server
  • To create radio groups
  • From JS/CSS when you need to reference multiple form controls at once (and when adding a class or using the element type is not more appropriate)

Use id

  • In the for attribute of the control's <label>
  • From JS/CSS when you need to reference a specific input

I suppose one advantage of this is that the lookup of that field via Javascript is faster through ids.

Not significantly (especially when the name is a unique one).

It sounds like the original designer hasn't been following standard conventions and has come up with something highly JavaScript dependant.

多孤肩上扛 2024-12-15 01:55:25

如果您使用表单,则应将 与表单元素一起使用。

label 上的 for 属性与 id 属性匹配,而不是与 name 属性匹配。

因此,您确实需要两者 id(用于标签等) name< /code> 用于服务器端代码。

If you're using forms, you should be using <label for="aFormElement"> along with your form elements.

The for attribute on label matches up with an id attribute, not a name attribute.

So, you really need both id (for the label, amongst other things) and name for server-side code.

堇年纸鸢 2024-12-15 01:55:25

为了加快查找元素的速度,您可以在表单上仅设置 id
然后对于字段,使用 name 来读取它们,如下所示:

var form = document.getElementById('theForm'),
    productName = form.productName.value;

For the speed to find your elements, you can set just id on the form.
Then for the fields use name to read them like:

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