使用 ids 而不是名称属性来标识表单中的字段有什么隐藏的原因吗?
我正在开发一个大型 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
name 和 id 执行不同的操作,虽然存在一些重叠,但对于它们执行的最重要的操作来说,它们不可互换。
使用名称
使用 id
的
for
属性中不显着(特别是当名称是唯一的时)。
听起来原始设计者并没有遵循标准约定,而是想出了一些高度依赖 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
Use id
for
attribute of the control's<label>
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.
如果您使用表单,则应将
与表单元素一起使用。
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 onlabel
matches up with anid
attribute, not aname
attribute.So, you really need both
id
(for thelabel
, amongst other things) andname
for server-side code.为了加快查找元素的速度,您可以在表单上仅设置
id
。然后对于字段,使用
name
来读取它们,如下所示: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: