简体   繁体   English

单击选择时如何更改选择的“大小”?

[英]How to change “size” of select when click on select?

sorry for my bad english, i have a problem with select : 对不起,我的英语不好,我对select有问题:

<form name="reg" style="width:700px;" action="#" method="post">
 <p align="center"> 
    <select onChange="reg(this.options[this.selectedIndex].value)" size="1">
        <option selected value="250">Select the Reg</option> 
        <option value="0">Reg 1</option>
        <option value="1">Reg 2</option>
        <option value="2">Reg 3</option>
        <option value="3">Reg 4</option>
        <option value="4">Reg 5</option>
        <option value="5">Reg 6</option>
        <option value="6">Reg 7</option>
        <option value="7">Reg 8</option>
        <option value="8">Reg 9</option>
        <option value="9">Reg 10</option>   
    </select>        
 </p>   
</form> 

I would like that when i click on select, the size of select changes in size="5" , because when the document load the size is size="1" (and this is good) but if i click on select, it shows all ten options, and this is the problem... While, if i click on select, the size changes, is most beautiful. 我想当我单击select时,select的大小将更改为size="5" ,因为在文档加载时,大小为size="1" (这很好),但是如果我单击select,它将显示所有十个选项,这就是问题所在...而如果我单击“选择”,则大小会更改,这是最漂亮的。

EDIT In future the form could have more 250 options, so is important that when the document load size is 1 , and when i click the size is 5 .... the size must be "5" only when the select show options, and when an option is selected the size must be 1. 编辑将来,表单可能会有250个以上的选项,因此重要的是,当文档加载大小为1时 ,当我单击大小为5 .... 仅当选择显示选项时,大小必须为“ 5”,并且选择一个选项时,大小必须为1。

The problem is that i don't know how made it, maybe with jquery? 问题是我不知道怎么做,也许用jQuery? or only with css? 或仅使用CSS?

Can you help me? 你能帮助我吗?

Thanks! 谢谢!

Here is a working solution for your problem: 这是您问题的可行解决方案:

  1. add focus event handler to the select list with id='selRegs' 将焦点事件处理程序添加到id ='selRegs'的选择列表中
  2. set the size attribute to 6 (5 will show 4 options + the "Select the Reg" option) 将大小属性设置为6(5将显示4个选项+“选择注册表”选项)
  3. after option is changed the size will be reset to 1 更改选项后,大小将重置为1
  4. remove focus from select 从选择中移出焦点

https://jsfiddle.net/juaxo8zm/8/ https://jsfiddle.net/juaxo8zm/8/

$(document).ready(function () {
                var initPos = $("#yourDiv").position();
                console.log("init: ");
                console.log(initPos);
                $('#selRegs').focus(function () {
                    $('#selRegs').attr("size", "6");

                    $("#yourDiv").css({
                        position: "absolute",
                        top: initPos.top,
                        left: initPos.left
                    });                    
                });
                $('#selRegs').change(function () {
                    console.log("selected");

                    $('#selRegs').attr("size", "1");

                    $("#selRegs").blur();

                    $("#yourDiv").css({
                        position: "static"
                    });
                });
            });

I use jQuery for this: 我为此使用jQuery:

.change() 。更改()

.focus() 。焦点()

.attr(param1, param2) .attr(param1,param2)

.blur() 。模糊()

.position() 。位置()

Use the HTML onclick ="myFunction()" event tag from the HTML select tag, or use jQuery click() handler if you want to declare the event handler at the javascript level (I mean if you want add the additional event from javascript). 使用HTML select标记中的HTML onclick =“ myFunction()”事件标记,或者如果您想在javascript级别声明事件处理程序,则使用jQuery click()处理程序(我的意思是如果您想从javascript中添加其他事件) 。 I mean you have a choice - add the extra event dispatch code in the HTML tag source code or add it from the javascript/jQuery level. 我的意思是您可以选择-在HTML标记源代码中添加额外的事件分发代码,或者从javascript / jQuery级别添加它。

The problem is that onChange() event dispatch doesn't get called until too late for your need (it gets called after a user selects a value). 问题在于,直到满足您的需求时才调用onChange()事件调度( 用户选择一个值之后调用)。

You need to get notified the moment the button for the drop-down menu is clicked so you can change the selected value (default) value. 单击下拉菜单的按钮时,您需要获得通知,以便可以更改所选值(默认值)。

In your click function handler change the selectedIndex property and set it to the the index of the line that contains "5" 在点击函数处理程序中,更改selectedIndex属性并将其设置为包含“ 5”的行的索引

You should use click event on select 您应该在选择时使用点击事件

$('select').on('click',function(){
    $(this).attr('size',5) 
});

Try this: 尝试这个:

<select size="1" 
    onfocus='this.size=5;' 
    onblur='this.size=1;' 
    onchange='this.size=1; this.blur();'
>

Just use javascript : 只需使用javascript:

<select id='sel' onfocus='changesize();' onchange='changesize2();' size='1'>
    <option selected value="250">Select the Reg</option>
    <option value="0">Reg 1</option>
    <option value="1">Reg 2</option>
    <option value="2">Reg 3</option>
    <option value="3">Reg 4</option>
    <option value="4">Reg 5</option>
    <option value="5">Reg 6</option>
    <option value="6">Reg 7</option>
    <option value="7">Reg 8</option>
    <option value="8">Reg 9</option>
    <option value="9">Reg 10</option> 
</select>

<script>
function changesize(){
    document.getElementById('sel').size = '5';
}
function changesize2(){
    document.getElementById('sel').size = '1';  
    document.getElementById('sel').blur();
}
</script>

Demo : http://codepen.io/anon/pen/zxbRZb 演示: http : //codepen.io/anon/pen/zxbRZb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM