2016-03-02 37 views
1

我有,显示我的产品和每一行我有一个按钮,删除这个数据我的JSP文件中的表格:警报上使用Javascript

<body> 
    <table> 
     <tr> 
      <td>ID</td> 
      <td>Name</td> 
      <td>Value</td> 
      <td>Quantity</td> 
      <td>Total</td> 
     </tr> 
     <c:forEach var="product" items="${list}"> 
      <tr> 
       <td>${product.id}</td> 
       <td>${product.name}</td> 
       <td>R$${product.value}</td> 
       <td>${product.quantity}</td> 
       <td>R$${product.quantity * product.value}</td> 
       <td><a href="showProduct?id=${product.id}">Edit</a></td> 
       <td><a href="deleteProduct?id=${product.id}">Delete</a></td> 
      </tr> 
     </c:forEach> 
    </table> 
    <br><br> 
    <a href="newProduct">New product</a> 
</body> 

我想创建当我点击删除警报按钮。

我知道我必须使用JavaScript,但是,如何在点击“确定”后重定向到我的控制器deleteProduct?

我想是这样的:

<script> 
if (confirm('Do you really want to delet this product?')) { 
    ??? 
} else { 
    alert('The product was not deleted'); 
} 
</script> 

谢谢!

回答

1

这里我的解决方案

function confirmDelete(aProductID){ 
 
if (confirm('Do you really want to delet this product?')) { 
 
    document.location.href='deleteProduct?id='+aProductID; 
 
} else { 
 
    alert('The product was not deleted'); 
 
} 
 
    }
<body> 
 
    <table> 
 
     <tr> 
 
      <td>ID</td> 
 
      <td>Name</td> 
 
      <td>Value</td> 
 
      <td>Quantity</td> 
 
      <td>Total</td> 
 
     </tr> 
 
     <c:forEach var="product" items="${list}"> 
 
      <tr> 
 
       <td>${product.id}</td> 
 
       <td>${product.name}</td> 
 
       <td>R$${product.value}</td> 
 
       <td>${product.quantity}</td> 
 
       <td>R$${product.quantity * product.value}</td> 
 
       <td><a href="showProduct?id=${product.id}">Edit</a></td> 
 
       <td><a href="#1" onclick="confirmDelete(${product.id})">Delete</a></td> 
 
      </tr> 
 
     </c:forEach> 
 
    </table> 
 
    <br><br> 
 
    <a href="newProduct">New product</a> 
 
</body>

+0

嘿人,它工作正常,非常感谢您的帮助:) –

2

你可以试试这个:

<td><a href="deleteProduct?id=${product.id}" onclick="return deleteProduct()">Delete</a></td> 


<script> 
function deleteProduct() 
{ 
    if (confirm('Do you really want to delet this product?')) { 
     return true; 
    } else { 
     alert('The product was not deleted'); 
     return false; 
    } 
} 
</script> 

如果return true<a>标签的onclick方法将转至该网址else如果你return false也不会......

+0

嗨brso05,感谢您的回复。不幸的是我尝试过,但是当我点击两个确定或取消产品被删除。我尝试了下面的解决方案,由@Rene Lykke Dahl提供,它工作正常,但是我真的非常感谢你的回复,很多感谢的人:) –

+0

@RaphaelTelatim亚,那是我的不好...我更新了现在应该工作的答案。 – brso05