如何将JavaScript数组发送到Form标签内的Servlet? [英] How to send JavaScript array to Servlet inside form tag?
问题描述
我已经创建了复选框代码,并在复选框上单击了我正在调用的函数,该函数会将传递的参数添加到JavaScript数组中. 我在主体内部使用了表单标签,而在表单标签内部只有复选框和一个按钮. 在按钮上单击servlet将被调用,因此我希望有一种机制也可以将Java脚本数组数据与复选框值一起发送到servlet.
I have made checkbox code and on checkbox click I am calling function which will add passed argument to JavaScript array. I have taken form tag inside body and inside form tag there is only checkboxes and a button. On button click servlet will called so I want some mechanism that will also send Java Script array data to servlet with checkbox value.
复选框的onClick我没有传递复选框的值,而是传递了其他详细信息,因此需要其他函数来在数组中添加数据并将其传递给servlet.
onClick of checkbox I am not passing checkbox value, I am passing other details so need other function to add data in array and pass it to servlet.
复选框代码:
<input type="checkbox" id="demo_box_2<%= aname %><%= aval %><%=k %>" class="css-checkbox" name="configcheckbox" value="<%= aval %>" onchange="addcategory('<%= acid %>')">
<label for="demo_box_2<%= aname %><%= aval %><%=k %>" name="demo_lbl_2<%=k %>" class="css-label"> <%= aval %></label>
JavaScript函数:
JavaScript function :
var acdata = [];
function addcategory(acid)
{
acdata.push(acid);
//alert(acid);
}
checkbox的代码在form标记内,所以我也想将具有checkbox值的acdata []数组发送到servlet,所以有什么帮助吗?
the code of checkbox is inside form tag, so I also want to send acdata[] array with checkbox value to servlet, so any help please ?
推荐答案
这是Santosh解释的示例代码,
Here is sample code as Santosh explained,
在您的脚本(已编辑)中,
var acdata = [];
function addcategory(acid)
{
acdata.push(acid);
$("#hidden_array").val(acdata);
}
在您的html中,
<input type="checkbox" id="demo_box_2<%= aname %><%= aval %><%=k %>"
class="css-checkbox"
name="configcheckbox" value="<%= aval %>" onchange="addcategory('<%= acid %>')">
<label for="demo_box_2<%= aname %><%= aval %><%=k %>" name="demo_lbl_2<%=k %>"
class="css-label"> <%= aval %></label>
这样,在表单中再添加一个隐藏字段,
With this add one more hidden field inside your form like this,
<input type="hidden" id="hidden_array" name="hiddenArray" >
因此,您可以像这样在jsp/servlet中获取数组值,
Hence you can get the array values in your jsp/servlet like this,
request.getParameterValues("hiddenArray");
PS:您可以使用字符串数组 String[]
处理数据.
PS: You can use string array String[]
to process your data.
这篇关于如何将JavaScript数组发送到Form标签内的Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!