How to pass objects in functions called in html Markup

Source: Internet
Author: User

Recently, jquery is used in combination with Ajax to develop a small and medium-sized website application. In the background management, you need to use Ajax to call the system function. Based on the Ajax return results, use jQuery to operate the front-end page.

For example, you can use Ajax to call the background function of information deletion. If success is returned, you can call this function to delete a row of information.

The general code is as follows:

Copy to ClipboardReference: [www.bkjia.com] $ (". delete"). click (function (){
If (confirm ("are you sure you want to delete? ")){
Var t = $ (this );

$. Ajax ({url: $ (t). attr ("href "),
Type: 'get ',
DataType: 'xml ',
Timeout: 1000,
Error: function (xhr, ajaxOptions, thrownError ){
Alert ("deletion failed! HTTP "+ xhr. status +" error occurred! "+ ThrownError );
},
Success: function (xml ){
Alert ("successful! ");
$ (T). parent (). parent (). remove ();
}});
}
Return false;
});

 

The link to be deleted is set as follows: <a class = "delete" href = "/article/delete/5">

However, a problem with this approach is that the article list should be displayed by page. If Ajax is used to display the next page, the function bound to the article will be invalidated when you click the hyperlink. Because the click function of. delete is bound after the page is loaded, if Ajax paging is used, the click function of. delete must be re-bound.

Sometimes you need to use the following method, <a onclick = "delete ()" href = "/article/delete/5">. The advantage of this method is that even if Ajax paging is used, you do not need to re-bind the click function of the link. However, when you delete an html tag on a page, you may encounter a problem. How do you transfer objects?

For Javascript experts, this kind of thing should be a simple thing. I found some information on the Internet and found that it was not what I wanted. Maybe it was hard to express my problem, after a bit of exploration, you can pass the object as follows: <a onclick = "delete (this)" href = "/article/delete/5">, in this way, you can pass the object a to the delete function when calling delete, and then delete the information on the front-end Page Based on the Ajax return results.

Source: blog

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.