Html <form action="#"> 和 <form method="post" action="#"> 做什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8395269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:50:01  来源:igfitidea点击:

what do <form action="#"> and <form method="post" action="#"> do?

html

提问by SandHawkerTech

I'm reading a book on html development (which I'm fairly new at) and despite the fact that the book just had its 1st publishing one month ago (Nov. 2011), the author is an experienced coder and maybe using #for the action in a form is old school?

我正在阅读一本关于 html 开发的书(我对这本书还很陌生),尽管这本书在一个月前(2011 年 11 月)才第一次出版,但作者是一位经验丰富的编码员,可能#用于形式上的行动是老派吗?

Because I'm trying to get the gist of the sample code and I cannot find an explanation of form action="#"despite searching for

因为我试图了解示例代码的要点,form action="#"尽管搜索了,但我找不到解释

<form action="#">   

on google, on SO, and in www.w3schools.com.

在 google、SO 和 www.w3schools.com 上。

Anyone know what the #action means for forms?

任何人都知道#表单的操作意味着什么?

回答by MEURSAULT

Action normally specifies the file/page that the form is submitted to (using the method described in the method paramater (post, get etc.))

Action 通常指定表单提交到的文件/页面(使用方法参数(post、get 等)中描述的方法)

An action of #indicates that the form stays on the same page, simply suffixing the url with a #. Similar use occurs in anchors. <a href=#">Link</a>for example, will stay on the same page.

一个动作#表示表单停留在同一页面上,只需在 url 后缀#. 类似的使用发生在锚点中。<a href=#">Link</a>例如,将停留在同一页面上。

Thus, the form is submitted to the same page, which then processes the data etc.

因此,表单被提交到同一页面,然后处理数据等。

回答by Shadow2531

action=""will resolve to the page's address. action="#"will resolve to the page's address + #, which will mean an empty fragment identifier.

action=""将解析为页面地址。action="#"将解析为页面的地址 + #,这意味着一个空的片段标识符。

Doing the latter might prevent a navigation (new load) to the same page and instead try to jump to the element with the id in the fragment identifier. But, since it's empty, it won't jump anywhere.

执行后者可能会阻止导航(新加载)到同一页面,而是尝试跳转到片段标识符中具有 id 的元素。但是,因为它是空的,所以它不会跳到任何地方。

Usually, authors just put #in href-like attributes when they're not going to use the attribute where they're using scripting instead. In these cases, they could just use action=""(or omit it if validation allows).

通常,当作者#不打算在使用脚本的地方使用属性时,他们只是放入类似 href 的属性。在这些情况下,他们可以只使用action=""(或在验证允许的情况下省略它)。

回答by Huntario

Apparently, actionwas required prior to HTML5 (and #was just a stand in), but you no longer have to use it.

显然,action在 HTML5 之前是必需的(并且#只是一个替身),但您不再需要使用它。

See The Action Attribute:

请参阅操作属性

When specified with no attributes, as below, the data is sent to the same page that the form is present on:

<form>

当没有指定属性时,如下所示,数据将发送到表单所在的同一页面:

<form>

回答by Jorren

The # tag lets you send your data to the same file. I see it as a three step process:

# 标签允许您将数据发送到同一个文件。我认为这是一个三步过程:

  1. Query a DB to populate a from
  2. Allow the user to change data in the form
  3. Resubmit the data to the DB via the php script
  1. 查询一个数据库来填充一个
  2. 允许用户更改表单中的数据
  3. 通过php脚本重新提交数据到DB

With the method='#' you can do all of this in the same file.

使用 method='#' 您可以在同一个文件中完成所有这些操作。

After the submit query is executed the page will reload with the updated data from the DB.

执行提交查询后,页面将使用来自数据库的更新数据重新加载。