Show/Hide different forms based on a option selected

后端 未结 7 1343
暗喜
暗喜 2021-02-04 19:28

I would like to know how to show/hide different forms based one form\'s selection.

In the sample code below all three forms are automatically set to display:none. I woul

7条回答
  •  灰色年华
    2021-02-04 20:24

    For future readers, this setup will show/hide those forms dynamically with no external libraries:

    function changeOptions(selectEl) {
      let selectedValue = selectEl.options[selectEl.selectedIndex].value;
      let subForms = document.getElementsByClassName('className')
      for (let i = 0; i < subForms.length; i += 1) {
        if (selectedValue === subForms[i].name) {
          subForms[i].setAttribute('style', 'display:block')
        } else {
          subForms[i].setAttribute('style', 'display:none') 
        }
      }
    }
    

    Then the html:

    
    
    
    
    
    
    ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
    

提交回复
热议问题