JavaScript 名称与 ID
据我所知,有两种方法可以从文本框中获取值,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
最终指南是 HTML 规范。
值得注意的是:
id
对任何 HTML 元素都有效,而name
仅适用于选定的少数元素(a
、input
、textarea
或许还有其他一些)。input
(和textarea
)元素需要设置name
。The ultimate guide in this is the HTML specification.
Things that stand out there:
id
is valid for any HTML element, whilename
only applies to a select few (a
,input
,textarea
and maybe a few others).input
(andtextarea
) element requires thename
to be set if you want it to be submitted.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. Theid
attribute is used to locate and element in the DOM. Usually the name is used only on the form elementsReferences:
http://www.w3.org/TR/html401/interact/forms.html
http://www.w3schools.com/html/html_forms.asp
我通常两者都用。正如您所提到的,
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 thename
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>
'sfor
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.我认为为每个表单元素指定名称和 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.
我更喜欢使用 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!