Making HTML5 datalist visible when focus event fires on input

前端 未结 4 1135
花落未央
花落未央 2021-02-04 00:11

As some might know already styling select element is a nightmare, literally impossible without some javascript trickery. The new datalist in HTML5 could serve the same purpose s

4条回答
  •  鱼传尺愫
    2021-02-04 00:53

    HTML:

    
    
    
       
    
       
       Input - Datalist
    
    
    
      
      

    jQuery:

    var result='';
    $(document).ready(function(){  
       $('input[type=text]').focus(function(){
          $('#categories option').each(function(){
          result=result+" "+$(this).val();
          });
    
          $('#result').show().html(result);
          $('input[type=text]').unbind('focus');
       });
       $('input[type=text]').blur(function(){
           $('#result').hide();  
           $('input[type=text]').focus(function(){
               $('#result').show();
           });
    
       });  
    });
    

    Working JS bin

    http://jsbin.com/urupit/4/watch

提交回复
热议问题