JS implements page turning and keeps the checkbox selected

Source: Internet
Author: User

The following requirements are required in the project: After pages are displayed, the status of the selected checkbox remains unchanged.

The project uses real paging. After you click the top or bottom page button each time, you can call the background method query again to refresh the page. So checkbox is false.

For example, in codoy music, the selected songs on the top and bottom pages are not retained. You can only select Add on each page and 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: When authorizing a role, select the module and the operations under the module, and click the checkbox on the top and bottom pages.

The interface is as follows:

The implementation idea is as follows:

In the interface, use pure Js. Concatenates the ID in the selected checkbox, including the module ID and operation ID, into a string, and then passes the string to the action.

Each time you call this page, you are preferred to call the selected ID string in action, and then splice the selected ID in this interface based on the ID string. Check the selected checkbox. If it is not in the ID container, load it. If it is not in the selected checkbox, check it. If it is in the ID container, delete it;

After the page is fully loaded, the checkbox in the interface is compared with the spliced ID string container. If it is in the string container, it is automatically selected in the checkbox. Otherwise, the value is false.


Because this idea is to use the spliced ID as a string, you need to pay attention to the syntax when obtaining the selected ID string in action in Js.

The implementation code is as follows:


   String ids= (String)request.getAttribute("ids");    if((ids==null)){ids="";    }

JS Code: Previous Page function:

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 + "," ;}// determine the operation under the 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 fully loaded, the JS Code is as follows:

Document. onreadystatechange = statechange; function statechange () {var IDs = "<% = IDs %>"; var checkedids = new string (IDS); If (document. readystate = "complete") {// loop all 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: During the test, the system always prompts that the function is undefined, not only prompting that the function is undefined on the next page, but prompting that all the buttons on the interface are undefined. So it took a long time to struggle. After solving the problem, share it with you.

In this case, there must be an error on the page. After JSP is parsed into HTML, there must be a syntax problem on the HTML page, leading to the inability to parse the HTML page.

A JavaScript code at the beginning: varids = <% = IDs %>;

When viewing the source file, I found that the code for the next page in JS is parsed as follows: varids =;

This syntax problem cannot be resolved, so it is unable to run.

The cause of this situation is: var IDs = <% = IDs %>; the ID container passed from the action is an empty string, so after parsing it, it becomes var IDs =;

Because the ID container is treated as a string, VAR IDs = "<% = IDs %>" is required. Even if the passed string is empty, the parsing result is as follows: var IDs = "";

Conclusion: When JavaScript Functions on the entire page cannot be executed, it must be indicated that JavaScript has a problem. the syntax of a JS function causes the entire page to fail to be parsed and run. If a JS function is not defined, the function name may be different from the function defined by the tag. If a character in a statement in a JS function is not defined, an undefined character is 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.