2013-10-06 63 views
2

我有下面的代碼,其中動態創建了Form秒的Input按鈕鏈接到一個文件delObservation.php,然後刪除該記錄。我想要包含一個Confirm函數並添加了此腳本。將出現Confirm消息框,但即使在確認消息框中單擊「取消」,delObservation.php仍會被調用。我究竟做錯了什麼?Javascript確認HTML表單不起作用

<script language="JavaScript" type="text/javascript"> 
function checkDelete(){ 
return confirm('Are you sure you want to delete this record?'); 
} 
</script> 

<form name="deleteReport" action="http://www.website/delRecord.php" method="Post"> 
    <input name="recordID" type="submit" 
      value="<?php echo$row['recordID'] ?>" class="delButton" 
      onclick="checkDelete()" > 
</form> 
+2

之後沒有空格您應該在'

'元素上使用'onsubmit'屬性,而不是按鈕上的'onclick'。 –

回答

3

嘗試改變input onclick事件:

//from 
onclick="checkDelete()" 
//to 
onclick="return checkDelete()" 

如果返回值是假的,將取消默認的動作。

例如:

//to do the default action regardless return value of the function 
onclick="checkDelete();" //Or 
onclick="checkDelete(); return true;" 

//to cancel the default action regardless return value of the function 
onclick="checkDelete(); return false;" 

//to decide the action depending return value of the function 
//Your requirement 
onclick="return checkDelete();" 
1

試試這個: -

<input name="recordID" type="submit" value="<?php echo$row['recordID'] ?>" 
class="delButton" onSubmit="return checkDelete()"> 
        ^^^^^^^ 
1

你需要一個return添加到onclick象下面這樣:

<input name="recordID" type="submit" value="<?php echo $row['recordID'] ?>" class="delButton" onclick="return checkDelete()"> 

檢查fiddle

另外,我注意到php echo