Reputation: 2700
I have some trouble in this code, I want to make a judge if the next().children()
div hasn't lengh, then do an ajax require. But it always make an ajax require even the div has content.
Here is the html code:
<noscript>
<style>
.more{display:none;}
</style>
</noscript>
<!-- main content -->
<p class="click hidden" rel="12345">View more</p>
<div class="more">
<h5>Relative post</h5>
<div class="relative-post"></div>
<!-- comment area -->
</div>
<!-- main content -->
<p class="click hidden" rel="23456">View more</p>
<div class="more">
<h5>Relative post</h5>
<div class="relative-post"></div>
<!-- comment area -->
</div>
Here is the jquery code:
jQuery(document).ready(function(){
$(".click").live('click',function() {
if($(this).next('.more').is(':visible')){
$(this).next('.more').css('display','none');
}else{
$(this).next('.more').css('display','block');
}
if($(this).next('.more').children('.relative-post:has(*)').length){
}else{
var $this = $(this);
var item_id = $this.attr('rel');
$.ajax({
url: "process",
dataType: "html",
type: 'POST',
data: 'post=' + item_id,
cache: false,
success: function(data){
$this.next('.more').children('.relative-post').html(data);
}
});
}
}
});
});
Upvotes: -2
Views: 79
Reputation: 2080
if ($(this).next('.more').children('.relative-post').html().length){
Upvotes: 0
Reputation: 318202
$(function() {
$(document).on('click', '.click', function() {
var next = $(this).next('.more');
next.toggle(!next.is(':visible'));
if (next.children('.relative-post:empty').length) {
var item_id = $(this).attr('rel');
$.ajax({
url: "process",
dataType: "html",
type: 'POST',
data: {post: item_id},
cache: false,
success: function(data) {
next.children('.relative-post').html(data);
}
});
}
});
});
Upvotes: 1
Reputation: 480
Replace the code
if($(this).next('.more').children('.relative-post:has(*)').length)
with following
if($(this).next('.more').children('.relative-post').html())
Upvotes: 0