Implementation of JS to keep checkbox selected after paging _javascript tips

Source: Internet
Author: User

The paging in the project uses true pagination, and after each click on the next Page button, call the background method query again and refresh the page again. So the checkbox is false.

For example, in cool dog music, the songs that are selected on the top and bottom pages will not be retained, only select Add on each page, and then switch to the next page.

But there are requirements in the project, so the implementation can only be completed.

The specific requirements of the project are as follows: to the role of authorization, select the module and the operation under the module, click on the page after the selected checkbox unchanged.

The interface is as follows:

The realization of the idea is as follows:

In the interface, use pure JS to complete. The ID in the selected checkbox, including the module ID, the operation ID, is spliced into a string, and the string is passed to the action.

Each time you call this page, you invoke the selected ID string in the action, and then, based on the ID string, stitch the ID selected in this interface again. Checked checkbox, need to judge, if not in the ID container, then load in; Unchecked checkbox, you need to judge, if the original in the ID container, you need to delete;

After the page is fully loaded, the checkbox in the interface is compared to the stitching ID string container, and the checkbox is automatically checked in the string container. otherwise to false.

Tip Description:

Because this idea is the stitching ID as a string, then in JS get action in the selected ID string, you need to pay attention to grammar.

The implementation of the idea code is as follows:

The ID container in Java to get the action:

Copy Code code as follows:

String ids= (String) request.getattribute ("IDs");
if ((Ids==null)) {
Ids= "";
}

JS Code: Previous page function:
Copy Code code as follows:

function _prepage ()
{
var ids= "<%=ids%>";
var checkedids= new String (IDS);

var modules = document.getelementsbyname ("module");
var operates = Document.getelementsbyname ("Operate");

for (var i = 0; i < modules.length; i++) {
if (Modules[i].type = = "checkbox" && modules[i].checked) {
if (Checkedids.indexof (modules[i].value) ==-1) {
Checkedids=checkedids+modules[i].value+ ",";
}
To determine the operation under a module
for (var j = 0; J < Operates.length; J + +) {
var Operateid = new String (operates[j].id);
Operateid = operateid.substring (0, Operateid.indexof (","));
if (Modules[i].value = = Operateid) {
if (Operates[j].type = = "checkbox" && operates[j].checked) {
if (Checkedids.indexof (operates[j].value) ==-1) {
Checkedids=checkedids+operates[j].value+ ",";
}
}

if (Operates[j].checked==false) {
if (Checkedids.indexof (operates[j].value)!=-1) {
Checkedids=checkedids.replace ((operates[j].value+ ","), "");
}
}
}
}
}

if (Modules[i].checked==false) {
if (Checkedids.indexof (modules[i].value)!=-1) {
Checkedids=checkedids.replace ((modules[i].value+ ","), "");
}
}
}

With (Document.forms[0])
{
action= "Roleauthoriedmanager!getmoduleoperatebysystem?roleid="
+document.getelementbyid ("Roleid"). Value
+ "&systemid=" +document.getelementbyid ("SystemID"). Value
+ "&pageno=" +<%=pagemodelmodule.getpreviouspagenumber ()%>
+ "&querystring=" +document.getelementbyid ("Searchbyid"). Value
+ "&ids=" +checkedids;
Method= "POST";
Submit ();
}
}

After the interface is completely loaded, the JS code is as follows:
Copy Code code as follows:

Document.onreadystatechange=statechange;
function StateChange ()
{
var ids= "<%=ids%>";
var checkedids= new String (IDS);
if (document.readystate= "complete")
{
Looping through all the controls
var inputs=document.getelementsbytagname ("input");
for (Var i=0;i<inputs.length;i++)
{
if (inputs[i].type== "checkbox")
{
if (Checkedids.indexof (inputs[i].value)!=-1)
{
Inputs[i].checked=true;
}
}
}
}
}

Note: In the test, but always prompt, the function is not defined, not only prompted the next page function is not defined, all the buttons on the interface all the prompts are not defined. So tangled up for a long time. Solve, take out share under.
In this case, there must be an error on the page. JSP parsing into HTML, the HTML page must have a syntax problem, resulting in this HTML page can not parse.
Start JS a code:varids=<%=ids%>;
When viewing the source file, I found that a code of the next page of JS is resolved as follows: varids=;
This grammatical problem, certainly cannot be resolved, so it has not been able to run.
The reason for this situation is: Var ids=<%=ids%>; from the action pass over the ID container is empty string, so after parsing into Var ids=;
Because the ID container as a string, so the need for Var ids= "<%=ids%>" even if the pass over is empty string, the results are as follows: Var ids= "";
Summary: Encountered the entire page of the JS function can not be executed, it is certain that JS has a problem, a JS function in the grammar problem, resulting in the entire page can not be resolved to run. If a JS function is undefined, it is possible that the function name is not the same as the function defined by the label. If a certain character in one of the JS functions is undefined, the undefined character is explicitly prompted.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.