用Javascript警告

时间:2016-03-02 15:58:12

标签: javascript jsp

我的JSP文件中有一个表格显示我的产品,每行都有一个删除这些数据的按钮:

<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>

谢谢!

2 个答案:

答案 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标记的onclick方法中<a>,则else如果return false它赢了,则会转到网址white-space: nowrap; ...

答案 1 :(得分:1)

这是我的解决方案

&#13;
&#13;
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');
}
  }
&#13;
<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>
&#13;
&#13;
&#13;