2

我有以下代码,它Form动态创建了许多 s,这些 s 具有Input链接到文件 delObservation.php 然后删除记录的按钮。我想包含一个Confirm函数并添加了这个脚本。出现Confirm消息框,但即使我在确认消息框上单击“取消”,delObservation.php 仍然会被调用。我究竟做错了什么?

<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>
4

3 回答 3

3

尝试更改input onclick事件:

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

如果返回值为 false,它将取消默认操作。

例如:

//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();" 
于 2013-10-06T08:50:58.423 回答
1

尝试这个:-

<input name="recordID" type="submit" value="<?php echo$row['recordID'] ?>" 
class="delButton" onSubmit="return checkDelete()">
                  ^^^^^^^
于 2013-10-06T08:52:38.093 回答
1

您需要在下面添加return一个onclick

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

检查小提琴

另外,我注意到 php 后面没有空格echo

于 2013-10-06T08:54:12.597 回答