JavaScript名称与ID [英] javascript name vs ID
问题描述
据我所知,有两种方法可以从文本框中获取值
As far as I know there are two ways to get the value from a textbox either
document.formName.textboxName.value;
或
document.getElementbyId('textboxId').value;
据我了解,使用表单名称将意味着我编写的代码更少,因为名称可用于发布数据和获取值(除了使用ajax).就像我只是使用标准表单发布的地方一样,我会使用 name
进行发布,但不能使用 id
吗?
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
?
例如在php中,我将使用
e.g. in php I would use
$_POST['texboxName'];
如果我在文本框中的位置和ID不能使用php获取值?
If I where to have and ID on the textbox I cannot get the value using php ?
推荐使用哪种标准方法,并且使用 name
浏览器友好?如有可能,请链接,谢谢.
Which is the standard recommened way of doing this, and is using name
browser friendly? Links if possible please, thanks.
推荐答案
此处的最终指南是 HTML规范.
突出之处:
-
id
对任何HTML元素均有效,而name
仅适用于少数几个(a
,input
,textarea
,也许还有其他一些.) -
input
(和textarea
)元素需要要设置的名称
已提交.
id
is valid for any HTML element, whilename
only applies to a select few (a
,input
,textarea
and maybe a few others).- An
input
(andtextarea
) element requires thename
to be set if you want it to be submitted.
这篇关于JavaScript名称与ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!