Get index of select option in an optgroup with jquery

后端 未结 7 1254
囚心锁ツ
囚心锁ツ 2021-01-17 14:10

I have the following select:


                        
    
提交评论

  • 2021-01-17 14:35

    The same thing in jquery way is also short and simple:

    var idx = $('#end').attr('selectedIndex');
    
    0 讨论(0)
  • 2021-01-17 14:39

    Erm... whyever not good old DOM methods? For a single-select:

    var idx= document.getElementById('end').selectedIndex;
    
    // or $('#end')[0].selectedIndex if you must
    

    Or, which will also work on multi-selects, get the option element node you're interested in and fetch option.index on it.

    This is massively faster and simpler than getting jQuery to process complex selectors.

    0 讨论(0)
  • 2021-01-17 14:43

    According to your problem returning all selected option's index value . you can try following code , may be help you.

    code:

    javascript code:

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js">
    

           $(function(){
    
                    $('#allcheck').click(function(){
                    var chk=$('#select_option >optgroup > option:selected');
                       chk.each(function(){
                           alert('Index: ' + $('option').index(this));
                    });
                });
       });});
    

    HtML CODE:

           <select multiple="multiple" size="10" id="select_option" name="option_value[]">
             <optgroup label="Morning"> 
                <option value="12:00a">12:00 am</option> 
                <option value="12:30a">12:30 am</option> 
                <option value="1:00a">1:00 am</option> 
                <option value="1:30a">1:30 am</option> 
            </optgroup> 
            <optgroup label="Evening"> 
               <option value="12:00p">12:00 pm</option> 
               <option value="12:30p">12:30 pm</option> 
               <option value="1:00p" selected="selected">1:00 pm</option> 
               <option value="1:30p">1:30 pm</option> 
        </optgroup>  
    


         <strong>Select&nbsp;&nbsp;<a style="cursor:pointer;" id="allcheck">All</a>
     </strong>
    
    0 讨论(0)
  • 2021-01-17 14:45

    Use the index() function to find an element within a set. Construct a set of all the options using $("#end option"). Find the selected option using the :selected pseudo-element. Note: indexes are 0-based.

    var options = $("#end option");
    var idx = options.index(options.filter(":selected"));
    
    0 讨论(0)
  • 2021-01-17 14:55
    //funcion para seleccionar con jquery por el index del select 
    var text = '';
    var canal = ($("#name_canal").val()).split(' ');
    $('#id_empresa option').each(function(i, option) {
            text = $('#id_empresa option:eq('+i+')').text();
            if(text.toLowerCase() == canal[0].toLowerCase()){
                $('#id_empresa option:eq('+i+')').attr('selected', true);
            }
        });
    
    0 讨论(0)
  • 提交回复
    热议问题