removing all options of select box except 1st option

前端 未结 8 2144
情书的邮戳
情书的邮戳 2020-12-29 01:20

I\'m trying to empty select options when:

id \"mClick\" is selected, id\'s \"sClick\", \"cClick\" and \"srClick\" will be emptied.

id \"sClick\" is selected,

8条回答
  •  情歌与酒
    2020-12-29 02:06

    The easiest way to clear the options in the way you're describing is by using options.length = 1. Also, you can leverage the fact that each drop down clears the ones that logically follow it, so that you only need to declare a single change handler.

    $('#lForm select').on('change', function() {
      if (this.selectedIndex > 0) {
        var $others = $(this).closest('table').find('select'),
        current = $others.index(this); // find current
    
        while (++current < $others.length) {
          // for each following drop down
          $others.get(current).options.length = 1;
        }
      }
    });
    

    Demo

    I'm not sure how you're going to repopulate the drop downs though :)

提交回复
热议问题