Overview screen Click the checkbox to get multiple ID values _javascript tips

Source: Internet
Author: User

In web development, you often encounter a check box before each record in the screen, and click to select the record to delete, modify, view and so on.

Modifications and views are obtained by retrieving the ID value of a record and passing it to the background for a query to get the various property values of the Record object and then to the screen.

I said the focus is to select multiple records after the bulk deletion, how to get multiple records of the ID value is the key to the problem . The first is the method of selecting the check box all in the JSP page.

The code is as follows:

function checkevent (name, allcheckid) { 
var allck = document.getElementById (allcheckid); 
if (allck.checked = = True) 
Checkall (name); 
else 
Checkallno (name); 
Select All 
function Checkall (name) { 
var names = document.getelementsbyname (name); 
var len = names.length; 
if (Len > 0) { 
var i = 0; 
for (i = 0; i < len; i++) 
if (!names[i].disabled) { 
names[i].checked = true; 
} 
}} All optional 
function Checkallno (name) { 
var names = document.getelementsbyname (name); 
var len = names.length; 
if (Len > 0) { 
var i = 0; 
for (i = 0; i < len; i++) 
names[i].checked = false; 
 

Call the JSP code for the method:

 
 

Where Choosefaqid is the Name property value of the check box, Checkall is the Name property value of the Header check box.

The front desk performs batch-specific JS code, needs to separate the selected record ID values by commas, and deletes the batch method code as follows:

function Batchdeletechfaq (idstr) { 
var ids= "'"; 
$ ("Input[name= ' choosefaqid ']"). each (function () { 
if (this.checked) { 
ids+=this.value+ ', ' ' 
} 
}); 
ids+= "'"; 
Ids=ids.replace (/, '/g, '); 
if (ids== "") { 
Jqdialog.alert ("<s:text name= ' Faqmanage.pleaseselectfaq '/> ')"; 
return; 
} 
$.ajax ({ 
type: "POST", 
URL: "Admin/faqmanageaction!batchdeletefaq.action", 
data: {choosefaqid:ids} , 
dataType: "Text", 
success:function (HTML) { 
if (html== "Success") { 
Jqdialog.alert ("<s:text Name= ' faqmanage.operationwassuccessful '/>, function () {window.location.href = Window.location.href.replace (/#/ g, '); 
} 
else { 
Jqdialog.alert ("<s:text name= ' Faqmanage.operationfailedpleasetrylater '/>");}} 
); 
}); 

Background gets a set of ID values returned by the foreground

 
 

The previously selected set of IDs was used in the following ways:

 
 

This allows you to get an array of strings directly, without any editing action required. Later for what reason to change into the present way a bit unclear.

Finally, the background Bo layer to perform the deletion of the code:

/** 
* Bulk Delete FAQ based on ID 
* @param faqanswer 
* @return 
/public void Batchdeletefaq (final String IDs) c7/>final String hql = "Delete from Faqanswer O where o.id in (" +ids+ ")"; 
Faqmanagerdao.execute (HQL); 
}

Summary, I used the AJAX submission, click the Bulk Delete button, the first to determine whether the user has at least selected the record, or pop-up prompts message. Select and click the Delete button to perform the background method. If you perform an operation failure the eject operation failed message box.

The above is a small series to introduce a list of screen click check box to get multiple ID value method, hope to help everyone, if you want to know more information please pay attention to cloud Habitat community website!

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.