Specific ideas and code for batch deletion using jquery Ajax

Source: Internet
Author: User

JS page jqueryCode : Copy code The Code is as follows: // JavaScript document
$ (Document). Ready (function (){
// Select all
$ ("# Allchk"). Click (function (){
$ ("Input [name = 'subchk']"). Prop ("checked", this. Checked );
});
// Single answer
VaR subchk = $ ("input [name = 'subchk']")
Subchk. Click (function (){
$ ("# Allchk"). Prop ("checked", subchk. Length = subchk. Filter (": checked"). length? True: false );
});
/* Batch Delete */
$ ("# Del_model"). Click (function (){
// Determine if at least one item is selected
VaR checkednum = $ ("input [name = 'subchk']: checked"). length;
If (checkednum = 0 ){
Alert ("select at least one item! ");
Return;
}
// Batch select
If (confirm ("are you sure you want to delete the selected project? ")){
VaR checkedlist = new array ();
$ ("Input [name = 'subchk']: checked"). Each (function (){
Checkedlist. Push ($ (this). Val ());
});
$. Ajax ({
Type: "Post ",
URL: "deletemore ",
Data: {'delitems ': checkedlist. tostring ()},
Success: function (result ){
$ ("[Name = 'subchk']: checkbox"). ATTR ("checked", false );
Window. Location. Reload ();
}
});
}
});
});
Page Elements :
<A href = "#" id = "del_model"> <span> delete a user </span>
<TH class = "tal"> <input type = "checkbox" id = "allchk"/> select all </Th>
<TD> <input type = "checkbox" name = "subchk" value = "$ {user. ID}"/> </TD>

The operation that needs to be performed after the request is completed. Here, the checkbox is removed (because the list loop of freemarker is used, and the checkbox sequence number changes after the data is removed, there are also unknown checkboxes selected and need to be removed ).Copy codeThe Code is as follows: Success: function (result ){
$ ("[Name = 'items ']: checkbox"). ATTR ("checked", false );
Window. Location. Reload ();
}

Java background code:Copy codeThe Code is as follows: @ requestmapping (value = "/deletemore", method = requestmethod. Post)
Public String deletemore (httpservletrequest request, httpservletresponse response ){
String items = request. getparameter ("delitems ");
String [] item = items. Split (",");
For (INT I = 0; I <item. length; I ++ ){
Userservice. Delete (integer. parseint (item [I]);
}
Return "Redirect: List ";
}

:

Related Article

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.