Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
191 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...