开发者

jQuery getting just added by ajax element

开发者 https://www.devze.com 2022-12-22 17:28 出处:网络
$.post(\'includes/script.php\', $(this).serialize(), function(data) { $(\'body\').append(data); }); alert ($(\'#new\').length)
$.post('includes/script.php', $(this).serialize(), function(data) {
    $('body').append(data);
});

alert ($('#new').length)

php script is <php echo "<div id="new">text</div>" ?>开发者_如何学Go;

it alerts 0, so it can't see new div.

How can you make it see new div?


You have to wait till your post is done before you can check the length. Do something like this:

$.post('includes/script.php', $(this).serialize(), function(data) {
    var newItem = $(data).appendTo('body');
    alert (newItem.length);
});


How are you calling it? I get alert 1 in Firefox, Chrome, and IE. Note that "function" was misspelled in your code snippet.

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript">

$(document).ready( function() {
addnew();
});

function addnew (){
    $('body').append('<div id="new">text</div>');
    alert ($('#new').length)
}
</script>
0

精彩评论

暂无评论...
验证码 换一张
取 消