gpt4 book ai didi

javascript - 将 jsp 页面的 jquery 函数中存在的数组值传递给 servlet

转载 作者:行者123 更新时间:2023-11-28 05:54:50 24 4
gpt4 key购买 nike

我需要帮助将 jsp 页面的 jquery 函数中存在的值传递到 servlet。

我已经使用 jquery 函数对选中的复选框行中存在的一组值进行了分组。现在我需要将这些值插入到我的数据库中。我尝试使用 request.getParameterValues() 传递值,但它不起作用。请帮助我将 jsp 页面的 jquery 函数中存在的存储数组值传递给 servlet。

这是我的 jsp 页面中的 jquery 函数

 <script>
$('#btn').on('click', function() {
var checkedRows = [];
$(':checkbox:checked').closest('tr').each(function() {
checkedRows.push(
$(this).find('td:gt(0)').map(function() {
return $(this).html();
}).get()
);
});
console.log( checkedRows );

});
</script>

JSP页面

<%@page import="java.util.List"%>
<%@page import="web.Products"%>
<%@page import="java.util.ArrayList"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type="text/javascript" src="JS/jquery-1.4.2.min.js"></script>

<script>
$('#btn').on('click', function() {
var checkedRows = [];
$(':checkbox:checked').closest('tr').each(function() {
checkedRows.push(
$(this).find('td:gt(0)').map(function() {
return $(this).html();
}).get()
);
});
console.log( checkedRows );

});
</script>

</head>
<form name="sform" method="post" action="Save_Products">
<table style="width:40%">
<tr> <th> Brand Name</th>
<th> Product Name</th>
<th> Description</th>
</tr>
<tr>
<td><%
List<Products> pdts = (List<Products>) request.getAttribute("productlist");
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"hidden\" name=\"brand_name\" value=\"" + prod.getBrandname() + "\">");
out.println("<br/>"+prod.getBrandname());

} %> </td>
<td><%
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"checkbox\" name=\"prod\" checked=\"checked\" value=\"" + prod.getProductname() + "\">" + prod.getProductname()+"<br>");
} } %> </td>
<td><%
if(pdts!=null){
for(Products prod: pdts){
out.println("<input type=\"text\" name=\"desc\" style=\"width:50px; height:22px\" value=\""+prod.getDesc()+"\"/><br/>");
}
}
} %> </td>
</tr>
<br/>
<br/>
<tr>
<td></td>
<td align="center"> <input id="btn" type="submit" value="Save" name="save"/> </td></tr>

</table>
</form>
</html>

最佳答案

尝试使用此代码将 js 数组传递给 servlet:

在你的jsp中添加:

$.post('Save_Products', {arrayData:checkedRows, mode:"insert"});

在您的 servlet 中:

String[] arrayData=request.getParameterValues("arrayData[]");

关于javascript - 将 jsp 页面的 jquery 函数中存在的数组值传递给 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789204/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com