Setting hidden datalist option values

前端 未结 5 698
旧巷少年郎
旧巷少年郎 2020-12-06 09:55

In the snippet below, I have two methods to choose an item: input with datalist and traditional select with options.

The select element keeps the option values hidde

5条回答
  •  死守一世寂寞
    2020-12-06 10:41

    The correct syntax of datalist is like bellow. Also there is no point to have two options with the same name. I took the JavaScript out of the HTML as it should be. ID attribute of the individual options can be replaced with other attribute.

    $(function() {
      $('input[name=chooseOption]').on('input',function() {
        var selectedOption = $('option[value="'+$(this).val()+'"]');
        console.log(selectedOption.length ? selectedOption.attr('id') : 'This opiton is not in the list!');
      });
    });
    
    
    
      

提交回复
热议问题