首页 > 解决方案 > 如何让确认框与 jsp 一起使用?

问题描述

所以每次我想从数据库中删除一条记录时,我想显示一个确认框,该框显示成功,但问题是无论我单击“确定”还是“取消”,记录都在删除

如何让它工作?谢谢

这是我的代码:

                         <table>
                                   <thead>
                                        <tr>
                                            <th>ID</th>
                                            <th>Date RDV</th>
                                            <th>Motif</th>
                                            <th>ID Patient</th>
                                            <th>Action</th>
                                        </tr>


                                        </thead>
                                        <p:forEach items="${liste }" var = "rdv">
                                    <tbody>

                                        <tr>
                                            <td>${rdv.id_Rdv }</td>
                                            <td>${rdv.date_Rdv }</td>
                                            <td>${rdv.motif }</td>
                                            <td>${rdv.id_P }</td>

                                            <td>
                                            <a href="${pageContext.request.contextPath}/RDVController?action=supprimer&Id_Rdv=${rdv.id_Rdv }" onclick="confirmation()" > Supprimer</a>
                                            </td>

                                        </tr>

                                    </tbody>                                                                              
                                  </p:forEach>                                       
                            </table>

这是我的javascript代码:

    function confirmation(){

    var result = confirm("vous êtes sûr ?");

    if(result){
        return true;
    }
    else{
        return false;
    }
}

标签: javascriptjspservlets

解决方案


您可以使用button而不是a标签并应用于css按钮,使其看起来像a标签。在这里,在下面的代码中,每当您单击button弹出窗口时,如果单击是,则会直接打开页面,否则页面将保持在同一页面上。

function confirmation(button) {
  //getting href value
  var href = button.getAttribute("href");
  console.log(href);
  var result = confirm("vous êtes sûr ?");

  if (result) {
    //if click yes redirect 
    window.location.href = href;
  } else {

    return false;
  }
}
.supprimer {
  background-color: transparent;
  text-decoration: underline;
  border: none;
  color: blue;
  cursor: pointer;
}

supprimer:focus {
  outline: none;
}
<table>
  <thead>
    <tr>
      <th>ID</th>
      <th>Date RDV</th>
      <th>Motif</th>
      <th>ID Patient</th>
      <th>Action</th>
    </tr>


  </thead>
  <p:forEach items="${liste }" var="rdv">
    <tbody>

      <tr>
        <td>${rdv.id_Rdv }</td>
        <td>${rdv.date_Rdv }</td>
        <td>${rdv.motif }</td>
        <td>${rdv.id_P }</td>

        <td>
          <button class="supprimer" href="${pageContext.request.contextPath}/RDVController?action=supprimer&Id_Rdv=${rdv.id_Rdv }" onclick="confirmation(this)"> Supprimer</button>

        </td>

      </tr>

    </tbody>
  </p:forEach>
</table>


推荐阅读