Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

HTML select form with option to search the dropdown list

 <input type="text" list="cars" />
<datalist id="cars">
  <option>Volvo</option>
  <option>Saab</option>
  <option>Mercedes</option>
  <option>Audi</option>
</datalist>

In the code above I would want to limit the users strictly to the options in datalist and prevent him/her from entering custom values. I cannot use select because the list is very long and fetaures of datalist like auto suggestion is really helpful.

question from:https://stackoverflow.com/questions/65873583/html-select-form-with-option-to-search-the-dropdown-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have you considered using JQuery? What you are trying to achive can easily be done like this

<input type="text" list="cars" id='disableTyping' />
<datalist id="cars">
<option>Volvo</option>
<option>Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</datalist>
<script type="text/javascript">
    $('#disableTyping').keypress(function(e) {
    return false;
});
</script>

And also if you decide to use this approach make sure to include JQuery in the <head></head> of your page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...