JavaScript 名称与 ID

发布于 2024-09-27 04:28:38 字数 475 浏览 6 评论 0原文

据我所知,有两种方法可以从文本框中获取值,

document.formName.textboxName.value;

或者

document.getElementbyId('textboxId').value;

据我所知,使用表单名称意味着我要编写的代码更少,因为名称可以用于发布数据和获取值(除了使用ajax) )。如果我只是使用标准表单发布,我会使用 name 发布,但我不能使用 id

例如,在 php 中,我会使用

$_POST['texboxName']; 

如果我在文本框上有 ID,我无法使用 php 获取值?

执行此操作的标准推荐方法是什么,并且使用 name 浏览器友好吗?如果可以请给个链接,谢谢。

As far as I know there are two ways to get the value from a textbox either

document.formName.textboxName.value;

or

document.getElementbyId('textboxId').value;

As I understand using form name would mean I have less code to write, as name can be used for posting data and getting the value (apart from using ajax). As where if I was just posting using a standard form I would use name to post but I cannot use id ?

e.g. in php I would use

$_POST['texboxName']; 

If I where to have and ID on the textbox I cannot get the value using php ?

Which is the standard recommened way of doing this, and is using name browser friendly? Links if possible please, thanks.

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

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

发布评论

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

评论(5

心如荒岛 2024-10-04 04:28:38

最终指南是 HTML 规范

值得注意的是:

  • id 对任何 HTML 元素都有效,而 name 仅适用于选定的少数元素(ainputtextarea 或许还有其他一些)。
  • 如果您希望提交input(和textarea)元素需要设置name

The ultimate guide in this is the HTML specification.

Things that stand out there:

  • id is valid for any HTML element, while name only applies to a select few (a, input, textarea and maybe a few others).
  • An input (and textarea) element requires the name to be set if you want it to be submitted.
那些过往 2024-10-04 04:28:38

name 属性在提交表单期间使用,即创建将被服务器处理的名称-值对。 id 属性用于在 DOM 中定位元素。通常该名称仅在表单元素上使用

参考:

http://www.w3 .org/TR/html401/interact/forms.html
http://www.w3schools.com/html/html_forms.asp

The name attribute is used during submitting the form, i.e. to create a name-value pair that will be processed to the server. The id attribute is used to locate and element in the DOM. Usually the name is used only on the form elements

References:

http://www.w3.org/TR/html401/interact/forms.html
http://www.w3schools.com/html/html_forms.asp

身边 2024-10-04 04:28:38

我通常两者都用。正如您所提到的,name 对于通过 JavaScript 使用数据以及提交数据后非常有用。请根据需要在 name 中随意使用其他字符:当输入将在数组服务器端使用时,我倾向于在名称中使用 []

id 属性可用于通过 JS/DOM 访问元素。它也被 for 属性使用。例如,如果您使用复选框执行此操作,则单击标签将导致该框被选中。这对于可用性来说是一个很大的优势。

I typically use both. As you mentioned, name is useful for using the data via JavaScript and also once it has been submitted. Feel free to use additional characters in the name as needed: I tend to use []s in my names when the input will be used in an array server-side.

The id attribute can be used for accessing the element via JS/the DOM. It is also used by <label>'s for attribute. If you do this with checkboxes, for example, clicking on the label will cause the box to become checked. That's a big plus for usability.

揽月 2024-10-04 04:28:38

我认为为每个表单元素指定名称 id 是一种很好的做法。原因是,如果您想添加标签或通过 sytlesheet 进行样式设置(您应该这样做),那么两者兼而有之。

至于使用 javascript 访问它:更好的方法是使用元素的 id,因为如果更改表单的名称,一切都会中断。当您使用 id 时,不会发生这种情况。

I consider it good practice to have a name and id for each form element. The reason being that if you want to add labels, or styling via sytlesheets (which you should be doing), then it makes sense to have both.

As for accessing it with javascript: the better way would be to go with the element's id, becuase if you change the name of the form everything breaks. This does not happen when you use the id.

他是夢罘是命 2024-10-04 04:28:38

我更喜欢使用 ID,因为代码的“功能”(即:您想要实现的目标)与语义无关。对我来说,.GetElementById('myid') 更突出,并且比仅将“myid”分散在代码中更具可读性。另外,您可以更轻松地重命名元素。这就是我的2便士价值!

I prefer using IDs, as the "function" (i.e.: what you're trying to achieve) of your code isn't tied up with in the semantics. For me, .GetElementById('myid') stands out more, and is more readable that just having "myid" scattered among the code. Plus you can more easily rename elements. That's my 2p worth!

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