如果中的操作字段有参数,会发生什么?

时间:2022-03-02 15:16:21

Is there a well-supported, common behavior that I can expect if I do something like this in HTML:

如果我在HTML中做这样的事情,是否有一种得到良好支持的、常见的行为呢?

<form method="get" action="/somePage.html?param1=foo&param2=foo">
  <input name="param2"></input>
  <input name="param3"></input>
</form>

Seems like this sort of thing is inherently ridiculous, but I've seen it used here and there and I was wondering what on Earth the expected behavior should be. Are browsers smart enough to tack on "&param2=whatever&param3=whatever" to the action, or do they just throw in a second question mark? Or what? Are there cases where this is actually the right way to do things?

看起来这类事情本质上是荒谬的,但我看到它到处被使用,我想知道究竟预期的行为应该是什么。浏览器是否足够聪明,可以将“&param2=whatever&param3=随便什么”添加到操作中,还是只添加第二个问号?还是别的什么?在某些情况下,这确实是正确的做法吗?

4 个解决方案

#1


52  

If the method attribute is set to GET, the browser drops the querystring parameters from the action attribute before constructing the form argument values.

如果将方法属性设置为GET,浏览器将从action属性中删除querystring参数,然后构造表单参数值。

So in your example, the request to the server on submit will look like: /somePage.html?param2=value&param3=value

因此,在您的示例中,对submit上的服务器的请求看起来是:/somePage.html

So no, when the method is "GET", as in your example, there's no reason to do this.

不,当方法是GET时,就像你的例子一样,没有理由这么做。

#2


17  

Not sure, but I think it's better practice to place those variables in hidden input fields. This way it doesn't matter if your posting method is either POST or GET.

不确定,但我认为最好将这些变量放在隐藏的输入字段中。这样,不管你的发布方法是POST还是GET都没有关系。

<form method="get" action="/somePage.html">
  <input name="param2"></input>
  <input name="param3"></input>
  <input type="hidden" name="param1" value="foo" />
  <input type="hidden" name="param2" value="foo" />
</form>

#3


4  

You could change the method attribute in the form to "POST" with script before posting the form, so there could be a use for the query string in the action. It hardly seems to be the best solution for anything, though.

在发布表单之前,可以将表单中的method属性更改为“POST”,并使用脚本,以便在操作中使用查询字符串。然而,这似乎并不是解决任何问题的最佳方案。

#4


3  

Well, all the questions have been answered except the last, to which the answer is yes. For POST, it's allowed, but you may well find cases where it doesn't work. I've seen web servers that only allow postdata or querystring, so it's not dependable.

好吧,除了最后一个问题,所有的问题都得到了回答,答案是肯定的。对于POST,它是允许的,但是你很可能会发现它不起作用的情况。我见过只允许postdata或querystring的web服务器,所以它不可靠。

#1


52  

If the method attribute is set to GET, the browser drops the querystring parameters from the action attribute before constructing the form argument values.

如果将方法属性设置为GET,浏览器将从action属性中删除querystring参数,然后构造表单参数值。

So in your example, the request to the server on submit will look like: /somePage.html?param2=value&param3=value

因此,在您的示例中,对submit上的服务器的请求看起来是:/somePage.html

So no, when the method is "GET", as in your example, there's no reason to do this.

不,当方法是GET时,就像你的例子一样,没有理由这么做。

#2


17  

Not sure, but I think it's better practice to place those variables in hidden input fields. This way it doesn't matter if your posting method is either POST or GET.

不确定,但我认为最好将这些变量放在隐藏的输入字段中。这样,不管你的发布方法是POST还是GET都没有关系。

<form method="get" action="/somePage.html">
  <input name="param2"></input>
  <input name="param3"></input>
  <input type="hidden" name="param1" value="foo" />
  <input type="hidden" name="param2" value="foo" />
</form>

#3


4  

You could change the method attribute in the form to "POST" with script before posting the form, so there could be a use for the query string in the action. It hardly seems to be the best solution for anything, though.

在发布表单之前,可以将表单中的method属性更改为“POST”,并使用脚本,以便在操作中使用查询字符串。然而,这似乎并不是解决任何问题的最佳方案。

#4


3  

Well, all the questions have been answered except the last, to which the answer is yes. For POST, it's allowed, but you may well find cases where it doesn't work. I've seen web servers that only allow postdata or querystring, so it's not dependable.

好吧,除了最后一个问题,所有的问题都得到了回答,答案是肯定的。对于POST,它是允许的,但是你很可能会发现它不起作用的情况。我见过只允许postdata或querystring的web服务器,所以它不可靠。