Small case of jquery Ajax request partial refresh

Source: Internet
Author: User

 

The parameters passed in the request's Ajax path (data) will be received by a variable with the same name (with the set get method) in the action. The returned data is a jquery array object, the data variables involved in the called action will be objects and will be encapsulated in data and finally returned to the page.

Case :,

 

I want to change the status. After jquery Ajax is used, the icon changes to partially refresh the page.

Principle: Partial refresh is part of the page refresh. In this case, only the icon changes are implemented, and the background code is separated from the icon in the foreground reality, instead of re-querying the database, second, after the digital display data in the background is modified, the foreground changes the icon directly.

 

1. The page provides a unique ID value for the icon of each record:

<TD align ="Center">

<S: If test ="Messagestate = 0">

<IMG src ="$ {CTX}/Images/04.png"Id ="R$ {Message. messageid}"/>

</S: If>

<S: else>

<IMG src ="$ {CTX}/Images/03.png"Id ="R$ {Message. messageid}"/>

</S: else>

</TD>

 

Ajax verification: add the id = aunread to the a tag, and then add the event

Jquery ("# aunread"). Click (Function(){

VaRStrids = ""; // defines a variable for data transmission.

$ ("Input [name = 'checkbox']"). Each (Function(){

If(This. Checked ){

Strids + =This. Value + ","; // you can obtain Multiple ID values and splice them into strings to upload them to the action.

}

});

$. Ajax ({

Type: "Post ",

Datatype: 'json', // accept the data format

Cache:False,

Data: "strids =" + strids,

URL: "$ {CTX}/feedbackonline/updatemessagestateunread. Action ",

Beforesend:Function(XMLHttpRequest ){

},

Success:Function(Data ){

VaRSTR = data. STR; // receives the returned data

For(VaRP
In
Str) {// traverse the accepted array object

VaRX = "# R" + STR [p]; // retrieve the icon ID of the record to be changed

$ (X). ATTR ("src", "$ {CTX}/images/04.png ");

// Change the src attribute value of the corresponding id value to the path of the corresponding icon

}

},

Error:Function(){

// Handle request errors

Alert ("error! ");

}

});

});

2. Background action:

PrivateString strids; // The set get method is omitted to automatically obtain the response data from the page.

PrivateString [] STR; // omitting the set get Method

@ Action ("/updatemessagestateunread ")

PublicString updatemessagestate ()ThrowsException {

String [] jstr = strids. Split (","); // splits the string into a string array.

STR = jstr; // assign the split string array to the array variable STR with the Get Set Method and return it to the page.

For(IntI = 0; I <jstr. length; I ++ ){

IntId = integer.Parseint(Jstr [I]);

Messageuserinfo = messageuserinfomanager. querybyid (ID );

Messageuserinfo. setmessagestate (0 );

Messageuserinfomanager. Update (messageuserinfo );

}

Return"Ajax ";

}

 

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.