javascript - ComboBox to select only data in the list but submit their 3 digit code HTML5 -
i want add input box in form users can select airport similar 1 in website (http://www.wego.pk/) when type on destination input, list of possible values detailed suggestion including city name + country name when user submits form value submitted 3 digit code airport.
i tried html5 combobox as:
<form> <input type="text" name="product" list="productname"/> <datalist id="productname"> <option value="isb">pen</option> <option value="khi">pencil</option> <option value="pwh">paper</option> </datalist> <input type="submit"> </form>
but if type pen no suggestion comes. kindly share code snippet or library purpose.
i recommend use autocomplete plugin (e.g. jquery ui autocomplete) instead html5 datalist. however, if question regarding datalist, can populate product code hidden field:
<form> <input type="hidden" name="productcode" id="productcode" value=""> <input type="text" name="product" id="product" list="productname" autocomplete="off"> <datalist id="productname"> <option data-code="isb">pen</option> <option data-code="khi">pencil</option> <option data-code="pwh">paper</option> </datalist> <input type="submit"> </form> <script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-zosebrlbnqzlpnkikedrpv7loy9c27hhq+xp8a4mxaq=" crossorigin="anonymous"></script> <script> $(function() { // use "bind" instead "on" jquery lt 1.7 $("#product").on("input", function(e) { var val = $(this).val(); var listid = $(this).attr("list"); var $option = $("#" + listid + " option").filter(function() { return ($(this).val() == val); }); var code = ($.type($option.data("code")) != "undefined" ? $option.data("code") : ""); $("#productcode").val(code); }); }); </script>
Comments
Post a Comment