jQuery .each() index?

后端 未结 5 2007
盖世英雄少女心
盖世英雄少女心 2021-01-31 01:03

I am using

$(\'#list option\').each(function(){
//do stuff
});

to loop over the options in a list. I am wondering how I could get the index of

5条回答
  •  余生分开走
    2021-01-31 01:29

    From the jQuery.each() documentation:

    .each( function(index, Element) )
        function(index, Element)A function to execute for each matched element.
    

    So you'll want to use:

    $('#list option').each(function(i,e){
        //do stuff
    });
    

    ...where index will be the index and element will be the option element in list

提交回复
热议问题