如果 <form> 中的操作字段会发生什么?有参数吗? [英] What happens if the action field in a <form> has parameters?

查看:118
本文介绍了如果 <form> 中的操作字段会发生什么?有参数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 HTML 中做这样的事情,我可以期待有没有得到充分支持的常见行为:

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

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

似乎这种事情本质上是荒谬的,但我已经看到它在这里和那里被使用,我想知道预期的行为到底应该是什么.浏览器是否足够聪明,可以将&param2=whatever&param3=whatever"添加到操作中,还是只是添加了第二个问号?或者是什么?在某些情况下,这实际上是正确的做事方式吗?

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?

推荐答案

如果方法属性设置为 GET,浏览器会在构造表单参数值之前从操作属性中删除查询字符串参数.

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

因此,在您的示例中,提交时对服务器的请求将如下所示:/somePage.html?param2=value&param3=value

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

所以不,当方法是GET"时,如在您的示例中,没有理由这样做.

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

这篇关于如果 &lt;form&gt; 中的操作字段会发生什么?有参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆