Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

javascript - Cancel action after cancel button on alert is clicked

Please, help me. I want to display confirmation box for delete action. If the user click okay then it will proceed. But if the user click cancel, it cancel the delete operation or go back to the same page. I did the code in javascript but it's not working. when I clicked on cancel on confirmation alert it still proceed to delete action.

  function confirmation() {
    if (confirm("Are you sure you want to delete the reservation?"))
      return true;
    else
      return false;
  }
<td>
  <a href="updateStaffServlet?action=update&sID=<c:out value=" ${staff.staffID} "/>"class="btn btn-icons btn-rounded btn-light">
    <i class="fa fa-pencil"></i>
  </a>
  <a onclick="confirmation()" href="deleteStaffServlet?action=delete&sID=<c:out value=" ${staff.staffID} "/>" id="dltbtn" class="btn btn-icons btn-rounded btn-light">
    <i class="fa fa-user-times"></i>
  </a>
</td>
question from:https://stackoverflow.com/questions/65897680/cancel-action-after-cancel-button-on-alert-is-clicked

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

this is not the best way but you can pass url to your confirmation function and if user confirmed, redirect him.

  function confirmation(url) {
    if (confirm("Are you sure you want to delete the reservation?"))
      location.replace(url);
  
  }
<td>
  <a href="updateStaffServlet?action=update&sID=<c:out" value=" ${staff.staffID} " class="btn btn-icons btn-rounded btn-light">
    <i class="fa fa-pencil"></i>
update link
  </a>
  <a onclick="confirmation('deleteStaffServlet?action=delete&sID=<c:out')" value=" ${staff.staffID} " id="dltbtn" class="btn btn-icons btn-rounded btn-light">
    <i class="fa fa-user-times"></i>
delete link
  </a>
</td>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...