从以上可以看出在alert的时候 数据并还没有加载出来 控制台也并没有打印出信息/所以此时是取不到数据的。

使用ajaxComplete()方法可以在请求完成时运行要执行的代码">
Home > Web Front-end > JS Tutorial > jQuery to get dynamically generated elements example_jquery

jQuery to get dynamically generated elements example_jquery

WBOY
Release: 2016-05-16 16:44:33
Original
1674 people have browsed it

Requirement description: Data can be added dynamically on the page, such as table, and rows can be added dynamically by clicking the button. Or when the page
is loaded, the table data is obtained from the background through ajax. And now we want to get one of the values, how to get it?

If you want to obtain it through an event such as click, mouseover, etc., you can use the live() method

Copy code The code is as follows:

$(".button").live("click",function(){
console.info($("#mytd") .html());
})

If it is not through an event, we have to get the value or perform other operations when the page is loaded

live( ) method will not work because we cannot pass in an event.

For example, the following code:
Copy code The code is as follows:

< 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
});


The above code is very simple, it is to add the value returned from the background through post to

The data returned by the background isAnd we want to get the id as mytd after post The value of

cannot be obtained at this time. We can observe the problem from the browser:
jQuery to get dynamically generated elements example_jquery
From the above, it can be seen that the data is not available when the alert is issued. After loading, the console does not print out the information, so the data cannot be obtained at this time.

Use the ajaxComplete() method to run the code to be executed when the request is completed. We modified it as follows:
Copy code The code is as follows:

$(function() {
$.post("admin/UserForumthemeBabygrowupFrontList.do",{},function(data){
console.info (data.table);
$("#tab").append(data.table);
})
$("#tab").ajaxComplete(function(){ //Pending request When completed, execute
alert($("#mytd").html());
})
});

jQuery to get dynamically generated elements example_jquery
At this time, The page has already loaded the data when getting it.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
BeijingShenzhen