如何使用GET方法阻止表单发送提交按钮的值?

时间:2022-11-23 17:33:00

I have a simple form:

我有一个简单的形式:

<form action="/search" method="get">
    <input type="text" name="q" value="">
    <input type="submit" name="search" value="search">
</form>

When submitting the url becomes `/search?q=Loremipsum&search=search

提交网址变为`/ search?q = Loremipsum&search = search

I really don't want that last bit, this seems pretty common problem and think it could be solved without js, but I realized that even google.com has this problem when you click on the search button. (maybe they don't care much about ugly urls?)

我真的不想要最后一点,这似乎是非常常见的问题,并认为它可以在没有js的情况下解决,但我意识到即使google.com也有这个问题,当你点击搜索按钮。 (也许他们不太关心丑陋的网址?)

search?hl=en&source=hp&q=Loremipsum&btnG=Google+Search&aq=f&..

Is there a way to prevent the value of the submit button to be excluded without javascript?

有没有办法防止在没有javascript的情况下排除提交按钮的值?

I see in Stack overflow the search is ?q= but they don't have a submit button.

我在Stack溢出中看到搜索是?q =但他们没有提交按钮。

2 个解决方案

#1


26  

You can omit name attribute in the final input like this:

您可以在最终输入中省略name属性,如下所示:

<form action="/search" method="get">
    <input type="text" name="q" value="">
    <input type="submit" value="search">
</form>

Should do the trick. Keeping value attribute allows you to manipulate what text is displayed on the button.

应该做的伎俩。保持值属性允许您操纵按钮上显示的文本。

#2


3  

For the record, you can also omit the submit button if you like, and the form will submit when you press return after typing your search term. (This is how the Stack Overflow search box works).

对于记录,您还可以根据需要省略提交按钮,并在键入搜索词后按回车键提交表单。 (这是Stack Overflow搜索框的工作方式)。

#1


26  

You can omit name attribute in the final input like this:

您可以在最终输入中省略name属性,如下所示:

<form action="/search" method="get">
    <input type="text" name="q" value="">
    <input type="submit" value="search">
</form>

Should do the trick. Keeping value attribute allows you to manipulate what text is displayed on the button.

应该做的伎俩。保持值属性允许您操纵按钮上显示的文本。

#2


3  

For the record, you can also omit the submit button if you like, and the form will submit when you press return after typing your search term. (This is how the Stack Overflow search box works).

对于记录,您还可以根据需要省略提交按钮,并在键入搜索词后按回车键提交表单。 (这是Stack Overflow搜索框的工作方式)。