2011-03-04 32 views
8

我有一個窗體裏面有一個圖像按鈕,這個圖像按鈕正在提交表單。我如何覆蓋這個功能?圖像按鈕裏面的表單,不想提交

<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="javascript:autofill();"/></form> 

回答

15

爲什麼不只是有圖像?返回false也會停止表單提交操作。

<form><input type="image" src="/images/arrow.png" id="imageButton" onClick="autofill();return false;"/></form> 
8

只需在您的autofill()函數後返回false即可。

onclick="autofill();return false;" 
6

爲什麼要使用輸入元素?我剛剛使用帶有onclick事件的圖像會更合適,如果你不希望引起任何提交或重置行爲

<image src="/images/arrow.png" onclick="javascript:autofill();" width="xxx" height="xxx" alt="Autofill" /> 
+0

基於問題的方式問這是正確的答案 – 2013-07-01 12:08:33

0

嘗試使用event.preventDefault放棄提交事件的思想點擊按鈕

例如:

$("#idButton").click(function(event) { 
    if(!valid) 
    { 
     //stopEvent 
     event.preventDefault(); 

    } else { 
     //submit your form 
     $("#form").submit(); 
    } 
}); 
0

你想禁用按鈕後,點擊提交按鈕/圖像?

如果你使用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>

請從我(jagb)讀this answer太多,如果你想使用的圖像沒有JavaScript中,需要css可以在那裏找到爲好。

2

點擊表格內的按鈕,默認提交表格。要改變它,你必須定義一個type =「button」的按鈕。然後,你可以在裏面添加img元素。這是我發現的最好方式。

<form> 
    <button type="button" id="imageButton" onclick="javascript:autofill();"> 
     <img src="images/arrow.png"> 
    </button> 
</form> 
+1

您好,歡迎來到堆棧溢出。正常用法是爲了解釋爲什麼你的代碼提供了OP問題所要求的解決方案。 – sweetfa 2015-04-18 21:03:29

+1

添加說明。感謝啓發。 – 2015-04-21 18:05:07