【问题标题】:how do I delete a product by id using JavaScript?如何使用 JavaScript 按 id 删除产品?
【发布时间】:2019-05-06 14:23:42
【问题描述】:

我需要帮助我正在尝试使用 jquery 从我的数据库中删除一个产品。我只使用 HTML 来实现它,现在我正在尝试使用 JavaScript 让它工作。我想既然我只是提交一个表单,我只会使用我工作过的 $("Formid").submit() 。但我工作的版本只是把我带到了另一个页面。有没有办法将表单的值放入按钮或对话框中,以便在我提交表单以删除产品时确保它正在检索 id?

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>

<script src="https://code.jquery.com/jquery-1.12.4.js"></script>

<script src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script type = "text/javascript">
function getProducts(){
    $.ajax(
        {
            type: "GET",
            url: "/SOSv8/api/products",
            dataType: "json",
            success: function(data)
            {
                $('#products').dataTable({
                    "data" : data,
                    "columns" : [{"data" : "name"}, {"data" : "descript"}, {"data" : "code"}, {"defaultContent" : "<button>delete</button>"}]
                });
            $('#products').on('click', 'button', function (){
                    $("#dialog").dialog(
                        {
                            width: '500px',
                            buttons: {
                                ok: function(){$("#delete").submit()},
                                cancel: function(){$(this).dialog('close')}
                            }

                    });
            });
            },
            Error: function (xhr, ajaxOptions, thrownError)
            {
                alert(xhr.status);
                alert(thrownError);

            }
        }   

    )
}
function dialog() {
    $( "#dialog" ).dialog(
        {
            width: '500px',
            buttons: {
                ok: function(){$("#delete").submit()},
                cancel: function(){$(this).dialog('close')}
            }
        }
    );
} 
$(document).ready(getProducts);
 </script>
        <table id="products" style="width:50%" border="1">
            <thead>
                <tr style="background-color:#A0A0A0">
                    <th><label>Name</label></th>
                    <th><label>Description</label></th>
                    <th><label>Code</label></th>
                    <th><label>Options</label></th>
                </tr>
            </thead>
        </table>

<div id="dialog" title="delete Code?" >
    <P> are you sure you want delete this code?</P> 
</div>

<form:form id="delete" method="POST"  modelAttribute="product" action="deleteProduct">
<form:hidden path="id" value="${product.id}" />
</form:form>

【问题讨论】:

  • 永远不要在 ajax 请求的 success: 回调中放置事件处理程序。
  • 阅读minimal reproducible example。您的问题包含很多您甚至自己评论过的代码都不相关。它似乎不包含您​​为 $("Formid").submit() 尝试过的代码。

标签: javascript jquery spring datatables


【解决方案1】:

您可以将一个函数传递给submit(),这样您就可以在提交表单之前进行所需的检查,事实上,如果您从回调函数中return false;,则表单永远不会被首先提交。所以你可以做一些验证一些console.log(id)来调试你的代码。

$('formId').submit(function(){
    // do some debugging here and return true to perform the submit or false to cancel
    console.log(idToDelete);
    return true;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2022-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多