如何使用图像类型按钮以 HTML 表单提交时删除 x 和 y?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/801702/
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
How to remove x and y on submit in HTML form with Image type button?
提问by Prashant
I have created a form in my application as follows:
我在我的应用程序中创建了一个表单,如下所示:
<form action="/search/" method="get">
<input id="search-box" name="search" type="text" size=30 title="Search" value="" />
<input id="search-submit" type="image" alt="Search" src="/images/search-button.gif" />
</form>
But when I am submitting my form then URL is created as below:
但是,当我提交表单时,会创建如下 URL:
mysitename.com/search/?search=hello&x=0&y=0
Can anyone please tell me why this x and y is coming in my URL. On more thing instead of image button if I am changing my form code as below then its working fine,
谁能告诉我为什么这个 x 和 y 出现在我的 URL 中。如果我正在更改我的表单代码,而不是图像按钮,那么它工作正常,
<form action="/search/" method="get">
<input id="search-box" name="search" type="text" size=30 title="Search" value="" />
<input id="search-submit" type="submit" value="Search"/>
</form>
but I need an image button to make my form look good. Please tell me how to remove these x and y parameteres from URL.
但我需要一个图像按钮来使我的表单看起来不错。请告诉我如何从 URL 中删除这些 x 和 y 参数。
回答by Eoin Campbell
You'll always get mouse co-ordinates for a submit button type="image"
您将始终获得提交按钮 type="image" 的鼠标坐标
You can use a standard submit type button and just apply styles to it to change the look.
您可以使用标准的提交类型按钮并对其应用样式以更改外观。
<input type="submit" id="search-submit" value=""
style="background-image: url(/images/search-button.gif); border: solid 0px #000000; width: WIDTHpx; height: HEIGHTpx;" />
回答by RichieHindle
They are the mouse coordinates of the click. I don't believe there's any way to prevent them - if you use an <input type='image'>
then you get them. Why is it a problem? Can't you just ignore them?
它们是点击的鼠标坐标。我不相信有任何方法可以阻止它们-如果您使用 an,<input type='image'>
那么您就会得到它们。为什么会出现问题?你就不能无视他们吗?
Answering Prashant's comment: Digg are adding an onclick
handler to the <input>
(or possibly an onsubmit
handler to the form) which builds the neat-looking search URL and redirects the browser to that URL, and then returns false
to prevent the <input>
from submitting the form itself. If you turn off JavaScript you'll see that you do get the x
and y
parameters in the URL. Clever!
回答 Prashant 的评论:Digg 正在向(或可能是表单的处理程序)添加一个onclick
处理程序,它构建整洁的搜索 URL 并将浏览器重定向到该 URL,然后返回以防止提交表单本身。如果您关闭 JavaScript,您将看到您确实在 URL 中获得了和参数。聪明的!<input>
onsubmit
false
<input>
x
y
回答by Chad Grant
document.getElementById("formid").submit = function() {
location = "/search/?search=" + encodeURIComponent( document.getElementById("search-box").value );
return false;
};
回答by DarkF
You can simply set a value, for an image button, this will override the regular coordinates behaviour. It worked for me.
您可以简单地为图像按钮设置一个值,这将覆盖常规坐标行为。它对我有用。
For example :
例如 :
<input type="image" value="submit" src="sup.png" name="supprimer" />
will return the php variable $_POST['supprimer']
and not its coordinate as image button.
Hope this trick will help you.
将返回 php 变量$_POST['supprimer']
而不是它的坐标作为图像按钮。希望这个技巧能帮到你。