One benefit of name
attributes is that they are included in the submitted form data by default.(name
属性的一个好处是,默认情况下, name
属性包含在提交的表单数据中。)
The code in your answer:(您的答案中的代码:)
<form name="emailForm1"> <input name="emailAddress1" type="email"><button type="submit">Submit</button> </form>
if you put ab@a
in the field, results in a submission that ends in ?emailAddress1=ab%40a
.(如果您在字段中输入ab@a
,则会导致提交结果以?emailAddress1=ab%40a
结尾。)
(Check your browser's Network tab to see.) As you can see, emailAddress1
is included in the query string by default - this can make things a lot easier in some situations, when you want the user to be able to submit the form directly to the server, without having to send the request yourself with Javascript.((检查浏览器的“网络”选项卡以查看。)如您所见,默认情况下,查询字符串中包含emailAddress1
在某些情况下,当您希望用户能够直接将表单提交到时,这会使事情变得容易得多。服务器,而不必自己使用Javascript发送请求。)
But if you're processing the fields on frontend only, or if you're always extracting field values and then sending a request manually, rather than submitting the form by default, then it doesn't matter - you can use any method to extract the values.(但是,如果仅在前端处理字段,或者始终提取字段值然后手动发送请求,而不是默认情况下不提交表单,那么这没关系-您可以使用任何方法提取价值。)
That said, global variables are highly discouraged, and IDs in the HTML always create global variables, so some might recommend using name
attributes (or another method of selecting elements) instead when possible.(也就是说,不建议使用全局变量,并且HTML中的ID总是创建全局变量,因此有些人可能建议在可能的情况下改用name
属性(或选择元素的另一种方法)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…