Html HTML表单标签中的target属性有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22660439/
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 01:23:11  来源:igfitidea点击:

What is the use of target attribute in HTML form tag?

htmlformstarget

提问by

While I was implementing a file upload progress bar in PHP, I saw this target attribute in formtag. The code was like this:

当我在 PHP 中实现文件上传进度条时,我在form标签中看到了这个目标属性。代码是这样的:

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" 
      method="POST" id="myForm" 
      enctype="multipart/form-data" 
      target="hidden_iframe">

What is the use of this target attributehere?

target attribute这里有什么用?

Actually, after submitting this form data (file) we track the upload progress from another page say upload.php. Could we implement this without the target attribute?

实际上,在提交此表单数据(文件)后,我们会从另一个页面跟踪上传进度upload.php。我们可以在没有目标属性的情况下实现这个吗?

回答by David Lin

That is used to specify in which window you would like to show the response from the remote server upon submitting your form.

这用于指定在提交表单时您希望在哪个窗口中显示来自远程服务器的响应。

Possible values are :

可能的值为:

  • _blank - new page
  • frame - show in the iframe with the given name
  • _self - show in the same iframe where the form locates
  • _parent - show in the parent page/iframe of the form's iframe
  • _top - the top most window
  • _blank - 新页面
  • frame - 在 iframe 中显示给定名称
  • _self - 在表单所在的同一个 iframe 中显示
  • _parent - 显示在表单 iframe 的父页面/iframe 中
  • _top - 最顶部的窗口

回答by Tuhin

<form action="demo_form.asp" method="get" target="_blank">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>

The target attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form.

target 属性指定一个名称或关键字,指示提交表单后收到的响应的显示位置。

The target attribute defines a name of, or keyword for, a browsing context (e.g. tab, window, or inline frame).

目标属性定义浏览上下文(例如选项卡、窗口或内联框架)的名称或关键字。

Target Attribute Values:

目标属性值:

_blank : The response is displayed in a new window or tab

_blank :响应显示在新窗口或选项卡中

_self : The response is displayed in the same frame (this is default)

_self :响应显示在同一帧中(这是默认值)

_parent : The response is displayed in the parent frame

_parent :响应显示在父框架中

_top : The response is displayed in the full body of the window

_top :响应显示在整个窗口中

framename : The response is displayed in a named iframe

framename : 响应显示在一个命名的 iframe 中

Now come to your code.

现在来看看你的代码。

method="POST" id="myForm" enctype="multipart/form-data" target="hidden_iframe"

indicates after posting the myForm the The response (resultant page) will be occupied by 'hidden_iframe'.

表示在发布 myForm 后,响应(结果页面)将被“hidden_​​iframe”占用。

回答by webeno

Works exactly the same way as anchor target. In your case, it looks like there is an iframe somewhere with name="hidden_iframe"- that's where the response from the form will be displayed.

与锚目标的工作方式完全相同。在您的情况下,看起来某处有一个 iframe name="hidden_iframe"- 这就是表单的响应将显示的地方。

Here is the description of form targets

这是表单目标的描述

A name or keyword indicating where to display the response that is received after submitting the form. In HTML 4, this is the name of, or a keyword for, a frame. In HTML5, it is a name of, or keyword for, a browsing context (for example, tab, window, or inline frame).

一个名称或关键字,指示在何处显示提交表单后收到的响应。在 HTML 4 中,这是框架的名称或关键字。在 HTML5 中,它是浏览上下文(例如,选项卡、窗口或内联框架)的名称或关键字。

Source: <form> - HTML | MDN #target

来源:<form> - HTML | MDN #目标