2014-05-09 40 views
1

我试图检测3个div是否为空,如果它们都有一些内容激活一个按钮。使用jQuery检测div中的内容

function checkContent() 
{ 

    if (!$.trim($('#Div1').html()).length && !$.trim($('#Div2').html()).length && !$.trim($('#Div3').html()).length) 
    { 
     console.log("Ready"); 
     $('#nextbutton').prop("disabled", false); 
    } 
    else 
    { 
     console.log("Not Ready"); 
    } 
} 

因此,我在Div1中添加了一些内容,并且控制台读取,未准备好。一些分区2还没有准备好。第3格(现在应该准备好了),但仍然说没有准备好。

不知道我在做什么错,有什么想法?

回答

2

删除不从下面

if ($.trim($('#Div1').html()).length > 0 && $.trim($('#Div2').html()).length > 0 && $.trim($('#Div3').html()).length > 0) { 
     console.log("Ready") 
     $('#nextbutton').prop("disabled", false); 
    } else { 
     console.log("Not Ready") 
    } 
1

您可以使用,而不是使一个长期条件筛选条件。

emptyDivs = $('[id^=div]').each(function(){ 
    return $.trim($(this).html()) == ""; 
}); 
if(emptyDivs.length == 0) 
    $('#nextbutton').prop("disabled", false); 
else 
    console.log("Not Ready")