如何使用JavaScript删除ID?

问题描述 投票:-3回答:1

我需要帮助我试图使用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>
javascript jquery spring datatables
1个回答
0
投票

你可以将一个函数传递给submit(),这样你就可以在提交表单之前检查你需要的wharever,事实上如果你从回调函数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;
});
© www.soinside.com 2019 - 2024. All rights reserved.