HTML select下拉列表如何显示VALUE / ID而不是文本

时间:2018-06-01 15:57:40

标签: javascript html select drop-down-menu

所以这是HTML代码:

<select id='selUser' style='width: 200px;'>
  <option value='0'>Select User</option> 
  <option value='1'>Yogesh singh</option> 
  <option value='2'>Sonarika Bhadoria</option> 
  <option value='3'>Anil Singh</option> 
  <option value='4'>Vishal Sahu</option> 
  <option value='5'>Mayank Patidar</option> 
  <option value='6'>Vijay Mourya</option> 
  <option value='7'>Rakesh sahu</option> 
</select>

我想要实现的是,当我点击Value&#39; 1&#39;时,下拉列表将显示&#34; 1&#34;而不是Yogesh辛格。有可能吗?

3 个答案:

答案 0 :(得分:4)

选择选项时,这将显示值而不是名称。我还写了一些代码,如果你再次更改它的名称,否则数字将保留在那里添加两个例子:

var oldIndex = ''
var oldText = ''

function change(x) {
  if (oldIndex != '' && oldText != '') {
    x.options[oldIndex].innerHTML = oldText
  }
  oldIndex = x.value
  oldText = x.options[x.selectedIndex].innerHTML
  x.options[x.selectedIndex].innerHTML = x.value
}

function change2(x) {
  x.options[x.selectedIndex].innerHTML = x.value
}
<select onchange="change(this)" id='selUser' style='width: 200px;'>
  <option value='0'>Select User</option>
  <option value='1'>Yogesh singh</option>
  <option value='2'>Sonarika Bhadoria</option>
  <option value='3'>Anil Singh</option>
  <option value='4'>Vishal Sahu</option>
  <option value='5'>Mayank Patidar</option>
  <option value='6'>Vijay Mourya</option>
  <option value='7'>Rakesh sahu</option>
</select>

<select onchange="change2(this)" id='selUser2' style='width: 200px;'>
  <option value='0'>Select User</option>
  <option value='1'>Yogesh singh</option>
  <option value='2'>Sonarika Bhadoria</option>
  <option value='3'>Anil Singh</option>
  <option value='4'>Vishal Sahu</option>
  <option value='5'>Mayank Patidar</option>
  <option value='6'>Vijay Mourya</option>
  <option value='7'>Rakesh sahu</option>
</select>

答案 1 :(得分:2)

这个脚本会这样做:

document.body.onload = function(){
    sel = document.getElementById('selUser');
    opts = sel.childNodes;
    for(var i in opts){
        opts[i].innerHTML = opts[i].value;
    }
}

答案 2 :(得分:2)

使用jQuery的解决方案:

&#13;
&#13;
var last_user_selected_name;

$(document).on('change', '#selUser', function() {
    $('#selUser option:not(:selected)').each(function() {
      if ($(this).text() === $(this).val()) {
        $(this).text(last_user_selected_name);
      }
    });

   var user_seleted = $('#selUser option:selected');

   if (user_seleted.val() != 0) {
       last_user_selected_name = user_seleted.text();
       user_seleted.text(user_seleted.val());
       console.log("User selected >> " + last_user_selected_name + 
                                " >> " + user_seleted.val());
   }
});
&#13;
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<select id='selUser' style='width: 200px;'>
  <option value='0'>Select User</option>
  <option value='1'>Yogesh singh</option>
  <option value='2'>Sonarika Bhadoria</option>
  <option value='3'>Anil Singh</option>
  <option value='4'>Vishal Sahu</option>
  <option value='5'>Mayank Patidar</option>
  <option value='6'>Vijay Mourya</option>
  <option value='7'>Rakesh sahu</option>
</select>
&#13;
&#13;
&#13;