jquery gets dynamically generated elements

Source: Internet
Author: User

Requirements Description: On the page can dynamically add data, such as table, click the button can dynamically add rows. Again or page

The table data is fetched through Ajax from the background at load time. And then we want to get one of those values, and how do we get it?

You can use the Live() method if you want to get through an event, such as Click,mouseover, and so on

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

And if 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.

such as 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 simple, 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 loaded out of the console also did not print out the information, so at this time is not able to get data.


using the Ajaxcomplete () method, you can run the code you want to execute when the request completes , and we modify it as follows:

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

at this point, the page has finished loading the data.




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.