Javascript确认以相同的形式删除和编辑

时间:2013-06-26 03:50:11

标签: php javascript html

我在同一表格上有两个提交按钮用于编辑和删除,我想要删除确认提醒。我已经尝试但是这只能工作,但我在删除确认中选择“否/取消”。我该怎么办 ?

这是表格

<form name="form" method="POST" action="update.php">
//form code
<input name="update_button" type="submit" value="Update" />
<input type="submit" name="delete_button" onClick="javascript:confirmDelete();" value="Delete"/>

这是javascript

<script type="text/javascript">
function confirmDelete()
{
    var status = confirm("Are you sure you want to delete ?");
    if(status)
    {
        parent.location.replace("parent.location='<?php echo "update.php";?>'");
    }else
    {
        parent.location.replace("parent.location='<?php echo "form.php?ticket=".$c['no'] ?>'");
    }
}
</script>

这是update.php

if(isset($_POST['update_button']))
{
    //update proses
}
else if(isset($_POST['delete_button']))
{
    //delete proses
}

我想删除确认显示“你确定要删除吗?”如果我选择“是”,它将从数据库中删除,如果我选择“取消”,它将保留在当前页面(form.php)。

之前感谢,

2 个答案:

答案 0 :(得分:1)

您无需在javascript:或任何事件处理属性中添加onclick。只需使用onclick="return confirmDelete();"

即可

,你的javascript将是这样的:

<script type="text/javascript">
  function confirmDelete() {
    var status  = confirm( "Are you sure you want to delete ?" );
    return status;
  }
</script>

或者您可以使用onclick="return confirm( 'Are you sure you want to delete ?' );"

答案 1 :(得分:0)

HTML:

<form name="form" method="POST" action="">
<input name="update_button" type="submit" value="Update" />
<input type="button" name="delete_button" id="delete_button" value="Delete"/>

使用Javascript:

$("#delete_button").on('click',function(){
  if(confirmDelete()){
    parent.location.replace("parent.location='<?php echo "update.php";?>'");
  }
  else{
      parent.location.replace("parent.location='<?php echo "form.php?ticket=".$c['no'] ?>'");
   }
});

function confirmDelete(){
  var status = confirm("Are you sure you want to delete ?");
    if(status)
    {
        return true;
    }
  return false;
}