当我单击复选框时,我要将复选框值与vin号一起传递给控制器,并且我想更新数据库中的复选框值 无需使用html表单中的提交按钮。如果有什么想法请帮助我 我正在使用拨动开关作为复选框
**This is my jquery**
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
$(document).ready(function(){
<!--$('#ans').val();-->
<!-- $("#ans").is(':checked') ? 1 : 0;-->
$('#ans').click(function(){
cb = $(this);
cb.val(cb.prop('checked'));
});
$('#ans').click(function(){
$.ajax({
type: "POST",
url : 'updatect',
data : $('form[name=upcat]').serialize(),
success : function(res) {
alert("insert");
if (res.validated) {
//Set respons
$('#resultContainer pre code').text(
$(AddCampaignResult));
$('#resultContainer').show();
} else {
//Set error messages
$.each(res.errorMessages, function(key, value) {
$('input[name=' + key + ']').after(
'<span class="error">' + value
+ '</span>');
});
}
}
});
});
});
$(document).ready(function(){
var data =eval('${dealercatalog}');
var table = $('#example').DataTable( {
"aaData": data,
"aoColumns": [
{ "mData": "imagelist"},
{"mData":"Selected"},
{"mData":"vehicleid"},
{ "mData": "vin"},
{ "mData": "make"},
{ "mData": "model"},
{ "mData": "type"},
{ "mData": "sellingprice"},
{"mData":"description"},
{"mData":"extcolor"}
]
});
});
</script>
<div class="loader"></div>
<div class="h3">
<span>${dealerLocation.getLocationName()}</span>
</div>
<div class="table-responsive">
<table id="example" class="display" style= "width:100%" border="1" cellspacing="2" style="overflow-x:auto">
<thead>
<tr>
<th>Image</th>
<th>Selected</th>
<th>VehicleID</th>
<th>Vin</th>
<th>Make</th>
<th>Model</th>
<th>Price</th>
<th>Type</th>
<th>Description</th>
<th>Ext Color</th>
</tr>
</thead>
<tbody>
<c:forEach var="dc" items="${Catalogs}">
<tr>
<td><img src="${dc.getFirstImage()}" height="100" width="100"/></td>
<td>
<c:url value="/updatect" var="url100"></c:url>
<form action="${url100}" name="upcat" enctype="multipart/form-data" method="POST">
<label class="switch">
<c:choose>
<c:when test="${dc.isSelected()==true}">
<input type="checkbox" id="ans" class="form-control" name="Selected"
value="${dc.isSelected()}" checked="checked" onclick="doAjaxPost()"/>
</c:when>
<c:otherwise>
<input type="checkbox" id="ans" class="form-control" name="Selected"
value="${dc.isSelected()}" onclick="doAjaxPost()"/>
</c:otherwise>
</c:choose>
<span class="slider round"></span>
</label>
</form>
</td>
<td>${dc.getVehicleid()}</td>
<td class="success">${dc.getVin()}</td>
<td>${dc.getMake()}</td>
<td>${dc.getModel()}</td>
<fmt:setLocale value="en_US"/>
<td><fmt:formatNumber value ="${dc.getSellingprice()}" type = "currency"/></td>
<td>${dc.getType()}</td>
<td>${dc.getDescription()}</td>
<td>${dc.getExtcolor()}</td>
<!--<td><img src="<c:url value="${dc.getFirstImage()}"/>" style="width:10%;"></td>-->
</tr>
</c:forEach>
</tbody>
</table>
</div>
</body>
</html>
这是我的控制人
@RequestMapping(value = "/updatect", method = RequestMethod.POST)
public ModelAndView UpdateWSHomeNetSelected(DealerCatalog dta,HttpServletRequest request)
{
UserDatabaseHelper udh=new UserDatabaseHelper();
DealerUser dUser = udh.Getuser(request.getUserPrincipal().getName());
int DealerID = dUser.getDealerId();
int result1=0;
ModelMap mm = new ModelMap();
//model.addAttribute("isUserClickedLogin", "true");
CatalogDatabaseHelper cdh = new CatalogDatabaseHelper();
result1=cdh.UpdateWSHomeNetSelected(dta.getVin(), dta.isSelected());
String strResult = "New Catalog could not be updated. Please check values and try again";
if(result1 != 0)
{
mm.addAttribute("successMsg","Catalog Added successfully");
}
else
{
mm.addAttribute("errorMsg","Catalog Not added Please Try Again");
}
List<DealerCatalog> cml = cdh.GetDealerCatalogData(DealerID);
mm.addAttribute("Catalogs", cml);
mm.addAttribute("catalogResult", strResult);
ModelAndView mv = new ModelAndView("catalog", mm);
return mv;
}