Jquery: Syntax error, unrecognized expression :nth-child()

前端 未结 1 1207
花落未央
花落未央 2021-01-21 09:04

I\'ve used someone else\'s answer to get this.

Jquery:

$(\"input:checkbox:not(:checked)\").each(function() {
    var columnName = $(this).attr(\'name\');         


        
1条回答
  •  情歌与酒
    2021-01-21 09:46

    I'm guessing you have one or more where the name attribute is not a number, or is not present.

    For example:

    
    

    When using your code, this outputs the error you described:

    Uncaught Error: Syntax error, unrecognized expression: :nth-child
    

    See fiddle

    A workaround would be to check that columnName is a valid number:

    var columnName = $(this).prop('name') || '0';
    
    if (columnName.match(/^[1-9]\d*$/)) {
      // columnName must start with a number between 1 to 9
      // and can be proceeded by zero or more numbers only
    }
    

    Also, it would be a better idea to assign a class to the checkboxes which are used to show/hide the corresponding td/th's.

    
    

    That way, you're only selecting/looping over the elements you want, and not every checkbox on the page.

    $('input.showHide').click(function() { ... });
    

    0 讨论(0)
提交回复
热议问题