Javascript没有返回false

时间:2014-03-11 04:07:44

标签: javascript jsp servlets

单击取消时,我的doDelete()函数没有返回false。单击确定后,它可以正常工作,我的数据可以删除。当我点击“取消”时,它会进入空白页面。我真的不知道我的代码有什么问题..

下面是我的javascript代码和我的删除图片:

<input type="image" src="img/delete.png" title="Delete User" name="delete" id="delete" onclick="doDelete(<%=rs.getInt("id")%>);"/>

function doDelete(id){      
    var r=confirm("Are you sure want to delete this user?");
    if (r==true){       
    document.forms["form"]["action"].value="delete";
    document.forms["form"]["recordId"].value=id;
    document.form.submit();     
    }   
    return false;
}

3 个答案:

答案 0 :(得分:1)

if(r == true) {
///you code
return true;
} else {
return false;
}

答案 1 :(得分:1)

r == true尝试最简单,最干净的方式时,您的代码路径不会返回任何值

var response = confirm("Are you sure want to delete this user?");
if (response) {

    document.forms["form"]["action"] = "delete";
    document.forms["form"]["recordId"].value = id;
    document.form.submit();
}

return response;

答案 2 :(得分:0)

您可以这样做:

<form onsubmit="return doConfirm();"...>

和功能:

function doConfirm() {
  var response = confirm('Do you really want to submit?');
  if (response) {
    // do stuff
  }
  return response;
}