确认删除按钮的消息

时间:2014-02-06 05:47:25

标签: javascript

下面的代码是我在JavaScript中的功能我想在点击删除按钮后确认消息。

的JavaScript

function deleteValues()
{

    var regno = document.getElementById('regs_numb').value;
    var patId =document.getElementById('Pat_Id').value; 
    if(regno ==""||isNaN(regno))
    {
         var el = document.createElement("div");
         el.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el.innerHTML = "please enter register number";
         setTimeout(function(){
          el.parentNode.removeChild(el);
         },2000);
         document.body.appendChild(el);
        return false;
    }
    else if(patId =="")
    {
         var el1 = document.createElement("div");
         el1.setAttribute("style","position:absolute;top:15%;left:80%;background-color:white;height:50px;width:150px;");
         el1.innerHTML = "please enter patient id ";
         setTimeout(function(){
          el1.parentNode.removeChild(el1);
         },2000);
         document.body.appendChild(el1);
         return false;
    }
    else
    {       
        document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
        document.forms[0].submit();
        return true;
    }
}

4 个答案:

答案 0 :(得分:1)

我会在confirm方法中检查Javascript的构建。

var s = confirm("Are you sure you want to do this?");
if(s){
  // Proceed
}else{
  // Uh oh, go back!
}

有关详细信息,请参阅此处:https://developer.mozilla.org/en-US/docs/Web/API/Window.confirm

答案 1 :(得分:1)

这是一个简单的解决方案

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


<input type="button" Onclick="ConfirmDelete()">

答案 2 :(得分:0)

尝试使用window.confirm()方法。

实施例

var r = confirm("Delete?");
if (r == true)
{
  //code to delete
}
else
{
  //cancel
}

了解更多信息 http://www.w3schools.com/jsref/met_win_confirm.asp

答案 3 :(得分:0)

而不是

else
{
    document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
    document.forms[0].submit();
    return true;
}

有类似

的内容
else
{
    var isSure = confirm('Are you sure you want to delete ' + patId + '?');

    if (isSure) {
        document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
        document.forms[0].submit();
        return true;
    }
}

这将使用 Ok Cancel 按钮弹出确认消息。仅当用户点击确定

时,表单才会提交

编辑:您可能也有错误

document.forms[0].action="deletePatient?regs_numb="+document.getElementById('regs_numb').value+"&&Pat_Id="+document.getElementById('Pat_Id').value;
                      // ------------------------------------------------------------------------^ You have 2 "&" symbols