我需要帮助,我正在尝试使用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>
答案 0 :(得分:0)
您可以将一个函数传递给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;
});