理解HTML中表单的基础知识

时间:2017-04-06 16:37:14

标签: html html5

HTML 1:

<html>
<body>

<form>
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form>   
</body>
</html>

HTML 2:

<html>
<body>

<form>
  First name:<br>
  <input type="text" name="firstname" value="Mickey">
  <br>
  Last name:<br>
  <input type="text" name="lastname" value="Mouse">
  <br><br>
  <input type="submit" value="Submit">
</form> 
<input type="email" value="email">
</body>
</html>

我知道<form>元素用于收集用户输入并将其传递给特定网址。在HTML 1中,当选择提交按钮时,<form>元素内的数据被收集并传递给提到的URL。

我在HTML 2中有相同的代码,但我在<input>元素之外添加了另一个<form>元素。

所以,我怀疑的是,当在HTML 2中选择提交按钮时<input>之外的<form>元素将会发生什么,输入的值是否会被读取?如果是或否是什么原因,因为如果我的理解是正确的submit<form>元素内,那么它将读取<form>中的值并忽略<input>元素放在外面。如果我错了请纠正我,也请解释。

3 个答案:

答案 0 :(得分:1)

When you submit the html form the inputs inside the tag form will be sents to the url in the action attribute of the form and the input that are outside of the form will be ignored because he are not in the same environment/context.

Some docs about form tag here

Regards

答案 1 :(得分:0)

表单外的输入值将不会被读取,因为它表单之外。表单仅包含<form></form>标记内的元素。

答案 2 :(得分:0)

不会读取表单标记之外的值 HTML是一种基于父子标记/节点工作的标记语言。所以这是必要的,因为可以有多个表单和许多输入字段嵌套在同一个文件中。

JQuery的
如果使用jquery,请使用表单标记的id并使用它来读取字段