Html 如何在表单上添加其他参数,使用 GET 方法提交

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

How to add additional parameters on a form, submitting it with GET method

html

提问by markzzz

I have this form :

我有这个表格:

<form method='GET' name='search' action='index.php?explore=search'> 
    <input type="hidden" name="searchType" value="all" />
    <input class="inputSearchSmall" name="search">
</form>
<a href="javascript:document.search.submit()"><img src="img/button_search.png" class="buttonSearch" /></a>

and I'd like to add parameters on the query string, after the action link. So, the result must be:

我想在查询字符串上添加参数,在操作链接之后。所以,结果必须是:

http://localhost:8080/website/index.php?explore=search&searchType=all&search=example

not :

不是 :

http://localhost:8080/website/index.php?searchType=all&search=example

what's the best way to do this? Adding a hidden param like :

这样做的最佳方法是什么?添加一个隐藏的参数,如:

<input type="hidden" name="explore" value="search" />

Or can I concatenate the parameters to the action script in some way?

或者我可以以某种方式将参数连接到动作脚本吗?

采纳答案by Todd

Adding them via a hidden param like you've suggested is the best way to go. It's more maintainable than adding to the form's action attribute value, and will do exactly what your asking. Just make sure you put it within the form tags.

像您建议的那样通过隐藏参数添加它们是最好的方法。它比添加到表单的 action 属性值更易于维护,并且会完全满足您的要求。只要确保你把它放在表单标签中。

回答by Oded

Either way will work - hidden input elements oradding parameters to the actionattribute of the formelement.

无论哪种方式都可以 - 隐藏输入元素向元素action属性添加参数form

There really isn't a "preferred" way - do what makes most sense to you, though if you want to add, delete and change parameters using hidden input elements will be easier to work with.

确实没有“首选”方式 - 做对您最有意义的事情,尽管如果您想使用隐藏的输入元素添加、删除和更改参数会更容易使用。