2016-03-02 151 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