In Java checkbox implements the method of multi selection across pages

  • 2020-06-01 09:44:35
  • OfStack

Recently, I want to realize one function, that is, checkbox cross-page multi-selection. After reading 1 time on the Internet, there is little information and most of it is incomplete. But through my hard work, I finally made it.

JSP page:

1. Define three Hidden variables:


<INPUT type="hidden" name="all_selected">  
<INPUT type="hidden" name="now_selected">  
<INPUT type="hidden" name="no_selected">  

2,javascript


//  To obtain checkbox information , The selected , uncheck , The currently selected   
    function getCheckBoxInformation() {  
     var checkboxes = document.getElementsByName("checkbox");  
     var checkedStr = "";  
     var uncheckedStr = "";  
     var url = "";  
     for(var i = 0; i < checkboxes.length; i++) {  
       var checkbox = checkboxes[i];  
       if(checkbox.checked) {  
        checkedStr = checkedStr + "," + checkbox.value;  
      }else {  
        uncheckedStr = uncheckedStr + "," + checkbox.value;  
      }  
    }  
    document.form1.now_selected.value = checkedStr;  
    document.form1.no_selected.value = uncheckedStr;  
   }  
 //  page onload Is used to calculate the selected item on the current page , And is represented on the page   
   function initPage() {  
     var all_selected = document.form1.all_selected.value;  
     if(all_selected != "" && all_selected!= null) {  
       var arrall_select = all_selected.split(",");  
       if(arrall_select.length > 0) {  
         for(var k = 0; k < arrall_select.length; k++) {  
            for(var i = 0; i < document.form1.checkbox.length; i++) {  
               if(document.form1.checkbox[i].value == arrall_select[k]) {  
               document.form1.checkbox[i].checked = true;   
             }   
           }  
         }   
       }   
     }  
    }  

The getCheckBoxInformation() method is called every time the page is turned, and the initPage() method is called when the page is loaded.

In the background, the method is called each time the page is turned


public String doubleSpread(String all_select, String now_selected, String no_selected) { 
    // Gets the currently selected item to join al 
    List al = new ArrayList(); 
    if(all_select != "" && all_select != null) 
    { 
      String[] all_select_str = all_select.split(","); 
      for(int a=0;a<all_select_str.length;a++) 
      { 
        al.add(all_select_str[a]); 
      } 
    } 
    // Adds the currently selected item to the list  
    if (now_selected != "" && now_selected != null) { 
      String[] all_now_select = now_selected.split(","); 
      for (int i = 1; i < all_now_select.length; i++) { 
        if (!al.contains(all_now_select[i])) { 
          al.add(all_now_select[i]); 
        } 
      } 
    } 
    // Removes the currently unselected item from the list  
    if (no_selected != "" && no_selected != null) { 
      String[] all_now_no_select = no_selected.split(","); 
      for (int i = 1; i < all_now_no_select.length; i++) { 
        if (al.contains(all_now_no_select[i])) { 
          al.remove(all_now_no_select[i]); 
        } 
      } 
    } 
    all_select=al.toString().replaceAll(" +","");; 
    all_select=all_select.substring(1,all_select.length()-1)+","; 
    return all_select; 
  } 

Upload the all_select returned by this method to the JSP page and assign the value to the hidden field. At this point, the function is implemented.


Related articles: