jquery gets dynamically generated elements

Source: Internet
Author: User

Description of requirements: The page can dynamically add data, such as table, click the button can dynamically join the row.

Again or page

When loading the table data is obtained through Ajax from the background. And then we want to get a value in it, and how to get it?

Suppose that you want to get an analogy click,mouseover through an event, and so on, you can use the Live() method

$ (". Button"). Live ("Click", Function () {       Console.info ($ ("#mytd"). html ());  
Specific instructions for live () http://blog.csdn.net/itmyhome1990/article/details/17380665

And assuming it's not an event, we're going to get the value or do something else when the page loads.

The live () method is not available because we cannot pass in an event.

For example, the following code:

<body><table id= "tab" border= "1" width= "30%" ></table></body><script type= "text/ JavaScript ">$ (function () {     $.post (" Admin/userforumthemebabygrowupfrontlist.do ", {},function (data) {   Console.info (data.table);   $ ("#tab"). Append (data.table);     })     Alert ($ ("#mytd"). html ());//Get value});</script>
The above code is very easy, that is, the value returned by post from the background is added to <table>

Background return data for <TR><TD id= ' mytd ' > Beijing </td><td> shenzhen </td></tr> and we're going to get a value with ID MYTD after post,

This is not available at this point, and we can see the problem from the browser:

From the above can be seen in the alert when the data is not yet loaded out of the console also did not print out the information, so this time is not data.


using the Ajaxcomplete () method to run the code to run when the request is complete , we change to such as the following:

$ (function () {$.post ("admin/userforumthemebabygrowupfrontlist.do", {},function (data) {console.info (data.table); $ ( "#tab"). Append (data.table);}) $ ("#tab"). Ajaxcomplete (function () {  //To Run alert ($ ("#mytd") when the request is complete. HTML ());});

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvaxrtewhvbwuxotkw/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast "/>
At this point, the page has loaded the data when it is retrieved.




jquery gets dynamically generated elements

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.