简体   繁体   English

jsp将js数组变量作为HTML中的附加参数传递<form action=“ ”>

[英]jsp Passing js Array variable as an Additional parameter in HTML <form action=“ ”>

I have a html table with lots of checkbox in the last column of the table and which looks like this 我在html table的最后一列中有一个带有很多checkboxhtml table ,它看起来像这样 在此处输入图片说明

The HTML for processing on click of submit button is like this 单击提交按钮时要处理的HTML就是这样

<form action="/chbs/adm/HallNamesValidation.jsp?chkb=jsFacilitiesArray" method="post">
  <table id="hallTable">
    <tbody>
      <td><input type="text" id="hall_location" name="hall_location" onchange="rowEdited($(this).parent())" class="form-control"></input>
      </td>
      <td scope="col" style="width: 125px; font-weight: normal;">
        <input type="checkbox" id="arrFacilities" name="arrFacilities" onchange="rowEdited($(this).parent())" class="facilityList"></input>
        <input type="checkbox" id="arrFacilities" name="arrFacilities" onchange="rowEdited($(this).parent())" class="facilityList"></input>
        <input type="checkbox" id="arrFacilities" name="arrFacilities" onchange="rowEdited($(this).parent())" class="facilityList"></input>
        <input type="checkbox" id="arrFacilities" name="arrFacilities" onchange="rowEdited($(this).parent())" class="facilityList"></input>
      </td>
    </tbody>
  </table>
</form>

The js code of rowedited() looks like this rowedited()js code如下所示

function rowEdited(thistd) {
    var facilityString = "";
    $(tr).find("td:eq(5) input:checkbox").each(function(){
        facilityString = facilityString  + $(this).is(':checked') + ', ';
    });
    jsFacilitiesArr [currentRow] = facilityString;

    currentRow = null;  
};

I want to pass the Array jsFacilitiesArr of the above js function along with the <form action=" ...> . I couldn't find a solution, so am requesting here for assistance. 我想将上述js函数的Array jsFacilitiesArr连同<form action=" ...>一起传递给我。我找不到解决方案,因此在这里寻求帮助。

PS: I Tried this but I know this is not correct PS:我尝试过这个,但是我知道这是不正确的

<form action="/chbs/adm/HallNamesValidation.jsp?chkb=jsFacilitiesArray" method="post">

将jsFacilitiesArr放在表单的数据属性中,并在rowEdited函数中读取它。

The Answer Given by @Pankaj is incorrect. @Pankaj给出的答案不正确。 I did more Googling and Searched more pages of Stackoverflow.com and arrived at the following solution. 我做了更多的谷歌搜索,然后在Stackoverflow.com的更多页面中搜索了以下解决方案。

just a line above <table> I placed the following HTML <table>上方的一行,我放置了以下HTML

<input type="hidden" id="jsFacilitiesArray" name="jsFacilitiesArray" value="" />

In js code of the rowEdited() function, I added the following statements 在rowEdited()函数的js代码中,我添加了以下语句

jsFacilitiesArr [currentRow] = facilityString;
$('#jsFacilitiesArray').val(JSON.stringify(jsFacilitiesArr));

In the HallNamesValidation.jsp , I have used following Statements to get the values HallNamesValidation.jsp ,我使用了以下语句来获取值

String facilitiesStr = request.getParameter("jsFacilitiesArray");
out.println(facilitiesStr);

That's It. 而已。 Nothing more, Nothing Less. 没事,没事。 No Need to Import any library for this. 无需为此导入任何库。 The Result as I desired is 我想要的结果是

["true, true, true, true, true, true, true, true, true, ","true, true, true, true, true, true, true, false, false, "]

To Convert this to an Array, I improvised the above code as follows: 为了将其转换为数组,我即兴创作了以下代码:

String facilitiesStr = (request.getParameter("jsFacilitiesArray")).replace(", \"]","").replace("[\"","");
        String[] facArr = facilitiesStr.split("\",\"");
        //out.println(facArr);
     for (int i = 0; i <  facArr.length; i++) {

        out.println( facArr[i] + " NEXT "); 
     }

You can update your form action attribute inside rowEdited function 您可以在rowEdited函数内更新表单操作属性

$("form#myForm").action = "/chbs/adm/HallNamesValidation.jsp?chkb=" + JSON.stringify(jsFacilitiesArr)

Please add some id or class to form to be easier to find it. 请在表单中添加一些ID或类,以便于查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM