最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is "name" attribute mandatory in <input>, <textarea> and <butt

programmeradmin1浏览0评论

Is "name" attribute mandatory in <input>, <textarea> and <button> elements? Or maybe we can use id or class instead?

Is "name" attribute mandatory in <input>, <textarea> and <button> elements? Or maybe we can use id or class instead?

Share Improve this question asked Jul 4, 2014 at 11:07 user3553682user3553682 3
  • name is not a pulsory attribute. we can use id or class instead... – user2138919 Commented Jul 4, 2014 at 11:08
  • instead what? do you mean for accessing control using jquery u want to use id or class ? – SHEKHAR SHETE Commented Jul 4, 2014 at 11:10
  • 1 Be carefull that AFAIK, the name attribute is used to correctly name POST variables sent to the server... not the id ! But if that's for client-side use only, better use ID I guess – Laurent S. Commented Jul 4, 2014 at 11:12
Add a ment  | 

4 Answers 4

Reset to default 6
  • If these tags are inside a form tag and you are subbmitting that form to a server, then name is required,
  • If you are just using them for client-side purposes and don't want to send them to server, then it is optional.

Name is not a required attribute. A small quote from the HTML spec:

The name content attribute gives the name of the form control, as used in form submission and in the form element's elements object. If the attribute is specified, its value must not be the empty string.

Notice the "if" in the quote, indicating that it is not required, from a standards perspective.

However, if the input is used with the browsers standard form submission, you won't be able to retrieve the value of the input on the server-side, if you don't have a name to refer to.

If you only need to retrieve the value on the client using JavaScript, then you can use an id, a class, or any other means to select the given input - in that case you can leave the name out if desired.

name is what gets sent to php scripts for processing so for example $_POST['Telephone'] when used as <input name="Telephone" type="text">. Not required unless being used for processing really.

No its not, but generally you would want it.

Try this

<?php
foreach($_GET AS $key=>$val)
{
    echo "name '$key'  has value '$val'<br>";
}
?>
<form>
<input type="text" name="abc">
<input type="text" id="a">
<input type="text" class="b">
<input type="text">
<input type="submit">
</form>

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论