简体   繁体   English

如何始终显示在HTML Select中选择的第一个选项?

[英]How to always show the first option as selected in HTML Select?

I've an HTML select where I want to achieve something when I select any option from the select menu it should not show those selected option into the box instead it has to show the first option always regardless of whatever user chose from the option inside. 我有一个HTML选择框,当我从选择菜单中选择任何选项时,我想在其中实现某些目标,它不应在框中显示那些选择的选项,而无论用户从内部选项中选择了什么,它都必须始终显示第一个选项。 Here is my CSS code for the div and Select: 这是我的div和Select的CSS代码:

   .styled select {
        background: transparent;
        width: 140px;
        font-size: 16px;
        border: 1px solid #ccc;
        height: 34px;
    }

    .styled {
        width: 40px;
        height: 34px;
        border: 1px solid #46b8da;
        border-radius: 3px;
        overflow: hidden;
        background-color: #5bc0de;
    } 

  <div class="styled">
     <select id="drpInsertMerge" onchange="drpChanged(this);">
            <option selected>{}</option>
            <option value="Phone Number">Phone Number</option>
            <option value="Email Address">Email Address</option>
            <option value="Given Name">Given Name</option>
            <option value="Family Name">Family Name</option>
            <option value="Display Name">Display Name</option>
      </select>
  </div>

Here, from the above code you can see that the dropdown box or the div is actually smaller than the select menu (which is required in my case) which matches with the first option {} but if I chose any other option from the below then most of the text is getting cut which is not good looking that's why I need your help or suggestion to know how to achieve this. 在这里,从上面的代码中您可以看到下拉框或div实际上小于与第一个选项{}匹配的选择菜单(在我的情况下是必需的),但是如果我从下面选择了其他任何选项,则大部分文字都被删掉了,看起来不好看,这就是为什么我需要您的帮助或建议来知道如何实现这一目标的原因。 Thanks. 谢谢。

Thanks everyone for taking time to read my question and for your valuable suggestion. 感谢大家花时间阅读我的问题和您的宝贵建议。 But here I've found a solution to my problem. 但是在这里,我找到了解决问题的方法。 Now, I'm using a javascript function which will make the selectedIndex = 0 after each selection made. 现在,我正在使用javascript函数,它将在每次选择后使selectedIndex = 0。

here is the code: 这是代码:

        function drpChanged(ddl) {
        var drpInsertMerge = document.getElementById("drpInsertMerge");

        var selectedText = drpInsertMerge.options[drpInsertMerge.selectedIndex].innerHTML;
        var selectedValue = drpInsertMerge.value;

        drpInsertMerge.selectedIndex = 0;

        return false;
    }

  <select id="drpInsertMerge" onchange="drpChanged(this);">
        <option selected>{}</option>
        <option value="Phone Number">Phone Number</option>
        <option value="Email Address">Email Address</option>
        <option value="Given Name">Given Name</option>
        <option value="Family Name">Family Name</option>
        <option value="Display Name">Display Name</option>
  </select>

Thanks again everyone. 再次感谢大家。

way that you are going to design is wrong.This might be a way to increase the width of your styled class in css. 您设计的方式是错误的。这可能是增加CSS中样式化类的宽度的一种方式。

  .styled select {
    background: transparent;
    width: 500px;
    font-size: 16px;
    border: 1px solid #ccc;
    height: 34px;
}

.styled {
    width: 300px;
    height: 34px;
    border: 1px solid #46b8da;
    border-radius: 3px;
    overflow: hidden;
    background-color: #5bc0de;
} 

  <div class="styled">
 <select id="drpInsertMerge" onchange="drpChanged(this);">
        <option selected>{}</option>
        <option value="Phone Number">Phone Number</option>
        <option value="Email Address">Email Address</option>
        <option value="Given Name">Given Name</option>
        <option value="Family Name">Family Name</option>
        <option value="Display Name">Display Name</option>
  </select>

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

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