Javascript to hide selected option

前端 未结 4 1327
伪装坚强ぢ
伪装坚强ぢ 2021-01-23 12:29

I have this code to hide selected options:

function connect()
{
    $(\".selectbox option\").show();
    $(\".selectbox\").each(function(i) { 
        var obj =          


        
4条回答
  •  别那么骄傲
    2021-01-23 13:17

    You already have the selected option in your selector - no need to find option - as that will return you an empty array

    $('.selectboxes option:selected').hide();
    // get rid of .find('option')
    

    To exempt this value.. I'm guessing you're referring to the selected value.. you can use :not as undefined stated

    $(".selectbox option:not(:selected)").show();
    

    You can take out the inline onChange since you are using jQuery.. and bind the change event handler to the select elements on dom ready

    $(function(){
        $('select').change(function(){   
           var $sel = $('option:selected'); // get all selected options
           $('option').show(); // show all options
           $sel.each(function(i,v){ // loop through selected options
               var $index = $(v).index(); // get index of selected option
               $('select').not($(this).parent()).each(function(){  // loop through other select - not current one       
                   if($index != 0){ // not default index
                       $(this).find('option').eq($index).hide(); // hide selected option in other dropdowns
                   }
               });
           });    
        }).change(); // <-- trigger change right away
    });
    

    http://jsfiddle.net/VE7jA/

提交回复
热议问题