2013-10-06 52 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