【发布时间】:2011-03-04 23:02:02
【问题描述】:
我有一个表单,里面有一个图像按钮,这个图像按钮正在提交表单。如何覆盖此功能?
<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="javascript:autofill();"/></form>
【问题讨论】:
我有一个表单,里面有一个图像按钮,这个图像按钮正在提交表单。如何覆盖此功能?
<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="javascript:autofill();"/></form>
【问题讨论】:
为什么不只有图像? return false 也会停止表单提交操作。
<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="autofill();return false;"/></form>
【讨论】:
在你的 autofill() 函数之后返回 false。
onclick="autofill();return false;"
【讨论】:
为什么要使用输入元素?如果您不想引起任何提交或重置行为,我认为仅使用带有 onclick 事件的图像会更合适
<image src="/images/arrow.png" onclick="javascript:autofill();" width="xxx" height="xxx" alt="Autofill" />
【讨论】:
点击表单内的按钮,默认提交表单。要更改它,您必须使用 type="button" 定义一个按钮。然后,您可以在其中添加 img 元素。这是我发现的最好的方法。
<form>
<button type="button" id="imageButton" onclick="javascript:autofill();">
<img src="images/arrow.png">
</button>
</form>
【讨论】:
尝试使用 event.preventDefault 在按钮点击时中止提交事件
例如:
$("#idButton").click(function(event) {
if(!valid)
{
//stopEvent
event.preventDefault();
} else {
//submit your form
$("#form").submit();
}
});
【讨论】:
您想在单击提交按钮/图像后禁用您的按钮吗?
如果您使用的是php,则可以使用下面的代码或添加disabled="disabled" 来禁用该按钮,只要您将其保留在输入表单中即可。
<?php if (isset($disable) && $disable === true) echo ' disabled="disabled"'; ?>
如何添加我的代码
如果您将代码行与图像类型一起使用:
试试:
<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="javascript:autofill();" <?php if (isset($disable) && $disable === true) echo ' disabled="disabled"'; ?> /></form>
如果您想使用不带 javascript 的图像,请也向我 (jagb) 阅读 this answer,也可以在那里找到所需的
css。
【讨论】:
向按钮标签添加属性 type="button" 解决了我的问题。
【讨论】: