【发布时间】: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