如何通过用户输入在屏幕上显示多个选项?

时间:2016-11-11 19:26:52

标签: javascript html

我有多个下拉菜单,但是当用户尝试选择多个选项时,屏幕上只显示1。此外,我希望选择显示在下拉菜单旁边,但它目前显示在下方。这是代码。谢谢!

<script type="text/javascript">
function showfield(name){
//------------------------------------------------------------
//If chosen show user input
//------------------------------------------------------------
  if(name=='GT')document.getElementById('div1').innerHTML='Greater Than: <input type="text" name="GreaterThan" />';
    if(name=='GE')document.getElementById('div2').innerHTML='Greater Equal to: <input type="text" name="GreaterEqualTo" />';
    if(name=='LT')document.getElementById('div3').innerHTML='Less Than: <input type="text" name="LessThan" />';
    if(name=='LE')document.getElementById('div4').innerHTML='Less Than Equal to: <input type="text" name="LessThanEqualTo" />';
    if(name=='EQ')document.getElementById('div5').innerHTML='Equal to: <input type="text" name="EqualTo" />';
    if(name=='B1')document.getElementById('div6').innerHTML='Between 1: <input type="text" name="Between1" />';
    if(name=='B2')document.getElementById('div7').innerHTML='Between 2: <input type="text" name="Between2" />';
    if(name=='NE')document.getElementById('div8').innerHTML='Not Equal to: <input type="text" name="NotEqualTo" />';
//------------------------------------------------------------
//If not selected dont show user input
//------------------------------------------------------------
  if(name!='GT')document.getElementById('div1').innerHTML='';
  if(name!='GE')document.getElementById('div2').innerHTML='';
    if(name!='LT')document.getElementById('div3').innerHTML='';
    if(name!='LE')document.getElementById('div4').innerHTML='';
    if(name!='EQ')document.getElementById('div5').innerHTML='';
    if(name!='B1')document.getElementById('div6').innerHTML='';
    if(name!='B2')document.getElementById('div7').innerHTML='';
    if(name!='NE')document.getElementById('div8').innerHTML='';
}

</script>

<select name="ChosenQualifiers[]" onchange="showfield(this.options[this.selectedIndex].value)" id="qual" multiple >
<option selected='selected'> Choose a Qualifier </option>
<option value='GT'> Greater Than </option>
<option value='GE'> Greater Equal to </option>
<option value='LT'> Less Than </option>
<option value='LE'> Less Than Equal to </option>
<option value='EQ'> Equal to </option>
<option value='B1'> B1 </option>
<option value='B2'> B2 </option>
<option value='NE'> Not Equal to </option>
</select>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<div id="div5"></div>
<div id="div6"></div>
<div id="div7"></div>
<div id="div8"></div>

2 个答案:

答案 0 :(得分:0)

问题是showfield函数只会收到1个值,而你正在清除所有以前的字段。因此,如果您考虑它,它不需要依赖于传入点击的名称,而是循环遍历选择中的所有元素并查看选择了哪些元素。

答案 1 :(得分:0)

divs变量只是一个非常小的改进,但主要原因是你要删除未选中的每个元素的innerHTML,并且你每次都这样做新的被选中。因此,使用旧代码,一次只能显示一个div,可以显示自定义html。

var divs = ['div1', 'div2', 'div3', 'div4', 'div5', 'div6', 'div7', 'div8'].reduce(function(prev, curr) {
      var obj = {};  //build an object that contains the ids as keys and dom elements as values so
      obj[curr] = document.getElementById(curr); //we don't have to keep searching the dom each time
      prev.push(obj);
    }, {});

function showfield(name){
//------------------------------------------------------------
//If chosen show user input
//------------------------------------------------------------
    if(name=='GT') divs['div1'].innerHTML='Greater Than: <input type="text" name="GreaterThan" />';
    else if(name=='GE') divs['div2'].innerHTML='Greater Equal to: <input type="text" name="GreaterEqualTo" />';
    else if(name=='LT') divs['div3'].innerHTML='Less Than: <input type="text" name="LessThan" />';
    else if(name=='LE') divs['div4'].innerHTML='Less Than Equal to: <input type="text" name="LessThanEqualTo" />';
    else if(name=='EQ') divs['div5'].innerHTML='Equal to: <input type="text" name="EqualTo" />';
    else if(name=='B1') divs['div6'].innerHTML='Between 1: <input type="text" name="Between1" />';
    else if(name=='B2') divs['div7'].innerHTML='Between 2: <input type="text" name="Between2" />';
    else if(name=='NE') divs['div8'].innerHTML='Not Equal to: <input type="text" name="NotEqualTo" />';
}

//if you want to be able to show more than one at a time you don't need to worry about
//removing things here