简体   繁体   中英

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 在此处输入图片说明

The HTML for processing on click of submit button is like this

<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

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.

PS: I Tried this but I know this is not correct

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

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

The Answer Given by @Pankaj is incorrect. I did more Googling and Searched more pages of Stackoverflow.com and arrived at the following solution.

just a line above <table> I placed the following HTML

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

In js code of the rowEdited() function, I added the following statements

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

In the HallNamesValidation.jsp , I have used following Statements to get the values

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

$("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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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