Creating a HTML5 datalist on the fly

后端 未结 4 1688
野的像风
野的像风 2021-01-20 03:43

I\'m trying to create a datalist on they fly and attach it to an existing input element. But nothing happens (no dropdown arrow is shown) jQuery would be acceptable, too.

4条回答
  •  有刺的猬
    2021-01-20 03:54

    try below solution using jquery on change of input text its appending options.

    $(document).ready(function() {
    	$("#search").on("input", function(e) {
    		var val = $(this).val();
    		if(val === "") return;
    			var arr = ['apple','banana','google','code','microsoft'];
    			var dataList = $("#searchresults");
    			dataList.empty();
    			if(arr.length) {
    				for(var i=0, len=arr.length; i").attr("value", arr[i]);
    					dataList.append(opt);
    				}
    			}
    
    	});
    });
    
    
    
    Example 1
    
    
    
    
    
    

提交回复
热议问题