JQuery ajax implementation.

Source: Internet
Author: User

Let's take a look at it first.

The top of this blog is to use a plug-in prepared by others. This effect was used last week and I went to study it. The following is a step-by-step implementation of the entire effect ....

Demo page implementation

XML/HTML code
  1. <DivClass = "digg" id = "digg">
  2. <DivClass = "good"> <Href = "#">
  3. <P>This document is good.</P>
  4. <DivClass = "bar">
  5. <DivId = "g_img" style = "width: 70%"> </Div>
  6. </Div>
  7. <SpanClass = "num" id = "num">70% (7000)</Span> </A> </Div>
  8. <DivClass = "bad"> <Href = "#">
  9. <P>Document to be improved</P>
  10. <DivClass = "bar">
  11. <DivId = "B _img" style = "width: 30%"> </Div>
  12. </Div>
  13. <SpanClass = "num">30% (3000)</Span> </A> </Div>
  14. </Div>

 

The main point is to control the g_img width by percentage. css code will not be pasted out.

 

Check the demo.

 

With the demo, other implementations are much more convenient. First, the page gets html. The page is loaded for the first time, and the background data is obtained using ajax instead of being directly displayed. (Asp is used as the background language for convenience of testing)

The following is the html code output by asp.

XML/HTML code
  1. Function getdigshtml () 'output html
  2. Dim rsajax, SQL, str, digsnum, undigsnum, digsnumall, digsper, undigsper
  3. Set rsajax = server. CreateObject ("adodb. recordset ")
  4. SQL = "select * from dig where id = 1"
  5. Rsajax. open SQL, conn, 1, 1
  6. Digsnum = rsajax ("digs ")
  7. Undigsnum = rsajax ("undigs ")
  8. If isnull (digsnum) then digsnum = 0
  9. If isnull (undigsnum) then undigsnum = 0
  10. Digsnumdigsnumall = digsnum + undigsnum
  11. If digsnumall = 0 then
  12. Digsper = 0
  13. Undigsper = 0
  14. Else
  15. Digsper = FormatNumber (cint (digsnum)/cint (digsnumall), 3) * 100
  16. Undigsper = FormatNumber (cint (undigsnum)/cint (digsnumall), (3) * 100
  17. End if
  18. Str = "<div class = 'good'>"
  19. Strstr = str &"<Href = JavaScript: isdigs ('digs ')>"
  20. Strstr = str &"<P>This document is good.</P> <DivClass = 'bar'> <DivId = 'G _ img 'style = 'width: "& digsper &" %'> </Div> </div>"
  21. Strstr = str &"<SpanClass = 'num'>"& Digsper &" % ("& digsnum &")</Span>"
  22. Strstr = str &"</A> </div> <divClass = 'bad'>"
  23. Strstr = str &"<Href = JavaScript: isdigs ('undigs ')>"
  24. Strstr = str &"<P>Document to be improved</P> <divClass = 'bar'> <DivId = 'B _ img' style = 'width: "& undigsper &" %'> </Div> </div>"
  25. Strstr = str &"<SpanClass = 'num'>"& Undigsper &" % ("& undigsnum &")</Span>"
  26. Strstr = str &"</A> </div>"
  27. Getdigshtml = str
  28. End function

 

After the output is complete, we will get the result from the front end. At this time, we will use jquery ajax. Why not use ajax directly? The reason is very simple. I won't ..... Let's take a look at the ajax code in jquery, which is very simple.

JavaScript code
  1. Function getdigshtml () // obtain the top level and step on the html
  2. {
  3. $. Ajax ({
  4. Type: 'post ',
  5. Url: 'digg. asp ',
  6. Data: 'Action = getdigshtml ',
  7. Success: function (msg ){
  8. $ ("# Digg" cmd.html (msg );
  9. }
  10. })
  11. }

 

The output is complete, and the next step is the digs and undigs operations, which is similar to getting html code.

JavaScript code
  1. FunctionIsdigs (digtype) // click it to perform the operation.
  2. {
  3. $. Ajax ({
  4. Type: 'post ',
  5. Url: 'digg. asp ',
  6. Data: 'Action = digs & digtype = '+ digtype,
  7. /* BeforeSend: function (){
  8. $ ("# Vote"). hide ();
  9. $ ("# Loadings"). show ();
  10. }. The ajax request displays the loading effect */
  11. Success:Function(Msg ){
  12. Switch(Msg)
  13. {
  14. /* The backend is used to determine
  15. Case '1 ':
  16. $ ("# Loadings"). hide ();
  17. $ ("# Vote"). show ();
  18. Alert ("Please log on first! ");
  19. Break;
  20. Case '2 ':
  21. $ ("# Loadings"). hide ();
  22. $ ("# Vote"). show ();
  23. Alert ("Download it first before proceeding! ");
  24. Break;
  25. Case '4 ':
  26. $ ("# Loadings"). hide ();
  27. $ ("# Vote"). show ();
  28. Alert ("You have participated in the evaluation! ");
  29. Break ;*/
  30. Case'3 ':
  31. Getdigshtml (); // rebind html
  32. // $ ("# Loadings"). Hide ();
  33. // $ ("# Vote"). Show ();
  34. Alert ("Thank you for your participation! ");
  35. Break;
  36. Default:
  37. }
  38. }
  39. })
  40. }

 

Commented out code: part of the code is used for legal verification of Background Data. The beforeSend method is related to the operations before the ajax request is executed (for loading more)

The last step is to re-obtain and bind the getdigshtml () after each data submission and return a successful result, thus ensuring the real-time data.

 

Let's take a look at the final effect. I will download the demo.

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.