jquery: if ul is empty

后端 未结 3 1471
猫巷女王i
猫巷女王i 2020-12-16 11:32

Okay I have a jQuery dialog box which has a form in it and I am at my wits end trying to figure this out... Lets see if I can verbalize what I am trying to do..

I ha

相关标签:
3条回答
  • 2020-12-16 11:52

    jQuery always returns an array of elements. If no matches were found, the array will be empty. An empty array in Javascript evaluates to true:

    console.log( !![] ); // logs: true
    

    You want to check the length of the returned set:

    if ( ! $("#mylist li").length ){
       $('#popuperrors').hide();
    }
    
    0 讨论(0)
  • 2020-12-16 11:53

    I like using the children() method because it reads nicely and it works even if you've already cached the selector for your ul. Here's what it looks like:

    $myList = $('#myList')
    if ( $myList.children().length === 0 ) ...
    
    0 讨论(0)
  • 2020-12-16 12:03
    if ($('#mylist li').length == 0) ...
    
    0 讨论(0)
提交回复
热议问题