dongxiong2000 2019-01-23 09:23
浏览 36

too long

Like i said in the title i want to keep a list hidden until another another list above has been selected, for example show student list after a class had been selected (so only student who belong to the selected class are shown)

<select name="class">
   <option value="">class1</option>
   <option value="">class2</option>
</select>

<select name="student">
  <option value="">student1</option>
  <option value="">student2</option>
</select>
  • 写回答

1条回答 默认 最新

  • dtfbj24048 2019-01-23 09:33
    关注

    Initially apply a hidden class (with display: none styling) to the second select list and then on the change of value (indicating a choice has been made) in the first select - remove the hidden class.

    EDIT - as as suggested by @HerrSerker - the event listener is now in the JS as opposed to inline in the HTML.

    Note that each select list has a selected disabled option - to allow for a blank option rather than the first option selected by default.

    // add event listener for change of class select list
    document.querySelector('select[name="class"]')
      .addEventListener('change', updateStudent)
    
    
    //function to remove the hidden class and styling of the second select list
    function updateStudent() {
     var el = document.querySelector('select[name="student"].hidden');
     el.classList.remove('hidden')
    
     }
    select[name="student"].hidden { 
     display:none
    }
    <select name="class">
      <option disabled selected></option>
       <option value="">class1</option>
       <option value="">class2</option>
    </select>
    
    <select name="student" class="hidden">
      <option disabled selected></option>
      <option value="">student1</option>
      <option value="">student2</option>
    </select>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100