2014-05-09 38 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")