问题
I currently have functionality on a page to give a user suggestions based on what they type in a text box (jquery suggest). The functionality I would like to add is to restrict what gets selected / entered into the text box to only be items in the list. Basically like a combobox which restricts its selections to what's in the list.
One thought I had is to verify that the entry in the textbox matches to an item in the list. I would have to alert the user that what is entered isn't allowed (or maybe there is a way to keep them typing other characters?), it's OK, but not real clean ui.
I figure someone on SO has done something like this; if someone has an idea to achieve this functionality please let me know.
The basic jquery I used:
<script type="text/javascript">
$(function() {
$('#autocomplete').autocomplete("getPcodes.aspx", { mustMatch: true, autoFill:true, multipleSeparator: "; ", minChars:3 });
});
</script>
There are a lot of nice options with the autocomplete. I was using a suggest jquery before, but I think I will opt for this one instead.
回答1:
The jQuery Autocomplete plugin lets you force the user to select an option from the returned dataset.
Check option mustMatch in http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions:
If set to true, the autocompleter will only allow results that are presented by the backend. Note that illegal values result in an empty input box.
来源:https://stackoverflow.com/questions/728596/jquery-google-type-suggest-with-restrict-to-list