如何在onfocus()上打开HTML select标签的选项列表

时间:2022-11-26 23:15:37

My HTML code looks like below.

我的HTML代码如下所示。

   <select id = "demo2">
        <option value = "---select---">---select---</option>
        <option value = "1">Oranges</option>
        <option value = "2">Apples</option>
        <option value = "3">Pears</option>
        <option value = "4">Kiwis</option>
        <option value = "5">Bananas</option>
        <option value = "6">Banansasas</option>       
        <option value = "7">Grapes</option>
   </select>

How do I open the option list of the select tag on onfocus() events?

如何在onfocus()事件上打开select标签的选项列表?

6 个解决方案

#1


6  

$(document).ready(function(){
    $('select').focus(function(){
        $(this).attr("size",$(this).attr("expandto"));
        var x = "select[tabindex='" + (parseInt($(this).attr('tabindex'),10) + 1) + "']";
        $(x).fadeTo(50,0);
    });
    $('select').blur(function(){
        $(this).attr("size",1); 
        var x = "select[tabindex='" + (parseInt($(this).attr('tabindex'),10) + 1) + "']";       
        $(x).fadeTo('fast',1.0);            
    });
});

Link for demo example

演示示例的链接

#2


2  

Here are the 2 javascript functions

这是2个javascript函数

function expand(obj)
{
    obj.size = 5;
}
function unexpand(obj)
{
    obj.size = 1;
}

then i create the select box

然后我创建了选择框

<select id="test" multiple="multiple" name="foo" onFocus="expand(this)" onBlur="unexpand(this)">
<option >option1</option>
<option >option2</option>
<option >option3</option>
<option >option4</option>
<option >option5</option>
</select>

#3


1  

HTML

   <select id = "demo2">
        <option disabled="true" selected="true" value = "---select---">---select---</option>
        <option value = "1">Oranges</option>
        <option value = "2">Apples</option>
        <option value = "3">Pears</option>
        <option value = "4">Kiwis</option>
        <option value = "5">Bananas</option>
        <option value = "6">Banansasas</option>       
        <option value = "7">Grapes</option>
   </select>

jQuery

$('#demo2').on('focus',function() {
    var optCount = $(this).children('options').length;
    $(this).attr('size', optCount);
});

jsFiddle Demo

#4


1  

Complete code for requirement like this question..for reference purpose..

像这个问题的完整的要求代码..供参考目的..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Auto-expand SELECT</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $(document).ready(function () {
    $('select').focus(function () {
        $(this).attr("size", $(this).attr("expandto"));

    });
    $('select').blur(function () {
        $(this).attr("size", 1);

    });
    $('select option').click(function () {
        $(this).parent().attr("size", 1);
        $(this).parent().val(this.value);

    });

    $('select').live('keypress', function (e) {
        var key = e.which;
        if (key == 13) // the enter key code
        {
            $(this).attr("size", 1);

        }
    });

});
</script>
<style type="text/css">
    select{position:absolute}
    .selectHolder{position:relative; height:45px}
    h4{margin:10px 0;}
    h5{margin:50px 0 0 0; text-align:center; font-weight:normal }
</style>
</head>

<body>

<div style="margin:30px auto;  width:400px; border:1px #888 solid; padding:20px; background-color:#F8F4F0">
<h3 style="text-align:center">Tab In, To Open Select Box</h3><br/><br/>
<form>
<input style="width:230px" type="text" name="start" value="Start here then tab to next input" tabindex="1" /><br/><br/> 
<h4>One to Seven</h4>
<div class="selectHolder">
    <select class="select" size="1" expandto="7" tabindex="2">
        <option value="1" selected="selected">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
        <option value="6">Six</option>
        <option value="7">Seven</option>
    </select>
</div>
<h4>One to Ten</h4>
<div class="selectHolder" >
    <select class="select" size="1" expandto="10" tabindex="3">
        <option value="1" selected="selected">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
        <option value="6">Six</option>
        <option value="7">Seven</option>
        <option value="8">Eight</option>
        <option value="9">Nine</option>
        <option value="10">Ten</option>    
    </select>
</div>
<h4>One to Four</h4>
<div class="selectHolder tab4">
  <select class="select" size="1" expandto="4" tabindex="4">
    <option value="1" selected="selected">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
  </select>
</div>
<h4>One to Dog</h4>
<div class="selectHolder tab4">
  <select class="select" size="1" expandto="5" tabindex="5">
        <option value="1" selected="selected">One</option>
        <option value="2">BAT</option>
        <option value="3">BIRD</option>
        <option value="4">CAT</option>
        <option value="5">DOG</option>    
  </select>
</div>

</form>


</div>
</body>
</html>

Find jsFiddle

#5


-1  

in your code you can place this code select id = "demo2" with this code select id = "demo2" onfocus="myFunction(this) fore highlight your Option.

在你的代码中你可以放置这段代码选择id =“demo2”,用这段代码选择id =“demo2”onfocus =“myFunction(this)高亮显示你的选项。

#6


-2  


  
  
  
$(document).ready(function(e) {
    $('#selectBox').focus(function() {
        $(this).attr('size',4);
        $(this).css('position',"absolute");
        $(this).css('x-index',"1000");
    });
    $('#selectBox').blur(function() {
        $(this).attr('size',1);
		$(this).css('position',"relative");
		$(this).css('x-index',"0");
    });
	$('#selectBox2').focus(function() {
        $(this).attr('size',4);
        $(this).css('position',"absolute");
        $(this).css('x-index',"1000");
    });
    $('#selectBox2').blur(function() {
        $(this).attr('size',1);
        $(this).css('position',"relative");
        $(this).css('x-index',"0");
    });
});
<div class="container" style="border:1px solid #00C;">
  <div class="row">
    <input type="text" placeholder="name" />
    <input type="text" placeholder="add" />
    <input type="text" placeholder="city" id="city" />
    <input type="text" placeholder="tel" />
    <input type="text" placeholder="home" />
    <input type="text" placeholder="cun" id="cun" />
    <br />
    <select id="selectBox">
      <option value="1" >1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <br />  
    <br />  
    <br />
    <select id="selectBox2">
      <option value="1" >1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>   
    </select>
  </div>
</div>

CLECK TO SEE JS FIDDLE

仔细查看JS FIDDLE

#1


6  

$(document).ready(function(){
    $('select').focus(function(){
        $(this).attr("size",$(this).attr("expandto"));
        var x = "select[tabindex='" + (parseInt($(this).attr('tabindex'),10) + 1) + "']";
        $(x).fadeTo(50,0);
    });
    $('select').blur(function(){
        $(this).attr("size",1); 
        var x = "select[tabindex='" + (parseInt($(this).attr('tabindex'),10) + 1) + "']";       
        $(x).fadeTo('fast',1.0);            
    });
});

Link for demo example

演示示例的链接

#2


2  

Here are the 2 javascript functions

这是2个javascript函数

function expand(obj)
{
    obj.size = 5;
}
function unexpand(obj)
{
    obj.size = 1;
}

then i create the select box

然后我创建了选择框

<select id="test" multiple="multiple" name="foo" onFocus="expand(this)" onBlur="unexpand(this)">
<option >option1</option>
<option >option2</option>
<option >option3</option>
<option >option4</option>
<option >option5</option>
</select>

#3


1  

HTML

   <select id = "demo2">
        <option disabled="true" selected="true" value = "---select---">---select---</option>
        <option value = "1">Oranges</option>
        <option value = "2">Apples</option>
        <option value = "3">Pears</option>
        <option value = "4">Kiwis</option>
        <option value = "5">Bananas</option>
        <option value = "6">Banansasas</option>       
        <option value = "7">Grapes</option>
   </select>

jQuery

$('#demo2').on('focus',function() {
    var optCount = $(this).children('options').length;
    $(this).attr('size', optCount);
});

jsFiddle Demo

#4


1  

Complete code for requirement like this question..for reference purpose..

像这个问题的完整的要求代码..供参考目的..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Auto-expand SELECT</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function(){
        $(document).ready(function () {
    $('select').focus(function () {
        $(this).attr("size", $(this).attr("expandto"));

    });
    $('select').blur(function () {
        $(this).attr("size", 1);

    });
    $('select option').click(function () {
        $(this).parent().attr("size", 1);
        $(this).parent().val(this.value);

    });

    $('select').live('keypress', function (e) {
        var key = e.which;
        if (key == 13) // the enter key code
        {
            $(this).attr("size", 1);

        }
    });

});
</script>
<style type="text/css">
    select{position:absolute}
    .selectHolder{position:relative; height:45px}
    h4{margin:10px 0;}
    h5{margin:50px 0 0 0; text-align:center; font-weight:normal }
</style>
</head>

<body>

<div style="margin:30px auto;  width:400px; border:1px #888 solid; padding:20px; background-color:#F8F4F0">
<h3 style="text-align:center">Tab In, To Open Select Box</h3><br/><br/>
<form>
<input style="width:230px" type="text" name="start" value="Start here then tab to next input" tabindex="1" /><br/><br/> 
<h4>One to Seven</h4>
<div class="selectHolder">
    <select class="select" size="1" expandto="7" tabindex="2">
        <option value="1" selected="selected">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
        <option value="6">Six</option>
        <option value="7">Seven</option>
    </select>
</div>
<h4>One to Ten</h4>
<div class="selectHolder" >
    <select class="select" size="1" expandto="10" tabindex="3">
        <option value="1" selected="selected">One</option>
        <option value="2">Two</option>
        <option value="3">Three</option>
        <option value="4">Four</option>
        <option value="5">Five</option>
        <option value="6">Six</option>
        <option value="7">Seven</option>
        <option value="8">Eight</option>
        <option value="9">Nine</option>
        <option value="10">Ten</option>    
    </select>
</div>
<h4>One to Four</h4>
<div class="selectHolder tab4">
  <select class="select" size="1" expandto="4" tabindex="4">
    <option value="1" selected="selected">One</option>
    <option value="2">Two</option>
    <option value="3">Three</option>
    <option value="4">Four</option>
  </select>
</div>
<h4>One to Dog</h4>
<div class="selectHolder tab4">
  <select class="select" size="1" expandto="5" tabindex="5">
        <option value="1" selected="selected">One</option>
        <option value="2">BAT</option>
        <option value="3">BIRD</option>
        <option value="4">CAT</option>
        <option value="5">DOG</option>    
  </select>
</div>

</form>


</div>
</body>
</html>

Find jsFiddle

#5


-1  

in your code you can place this code select id = "demo2" with this code select id = "demo2" onfocus="myFunction(this) fore highlight your Option.

在你的代码中你可以放置这段代码选择id =“demo2”,用这段代码选择id =“demo2”onfocus =“myFunction(this)高亮显示你的选项。

#6


-2  


  
  
  
$(document).ready(function(e) {
    $('#selectBox').focus(function() {
        $(this).attr('size',4);
        $(this).css('position',"absolute");
        $(this).css('x-index',"1000");
    });
    $('#selectBox').blur(function() {
        $(this).attr('size',1);
		$(this).css('position',"relative");
		$(this).css('x-index',"0");
    });
	$('#selectBox2').focus(function() {
        $(this).attr('size',4);
        $(this).css('position',"absolute");
        $(this).css('x-index',"1000");
    });
    $('#selectBox2').blur(function() {
        $(this).attr('size',1);
        $(this).css('position',"relative");
        $(this).css('x-index',"0");
    });
});
<div class="container" style="border:1px solid #00C;">
  <div class="row">
    <input type="text" placeholder="name" />
    <input type="text" placeholder="add" />
    <input type="text" placeholder="city" id="city" />
    <input type="text" placeholder="tel" />
    <input type="text" placeholder="home" />
    <input type="text" placeholder="cun" id="cun" />
    <br />
    <select id="selectBox">
      <option value="1" >1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <br />  
    <br />  
    <br />
    <select id="selectBox2">
      <option value="1" >1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>   
    </select>
  </div>
</div>

CLECK TO SEE JS FIDDLE

仔细查看JS FIDDLE