动态创建HTML5数据列表

时间:2018-09-19 03:39:57

标签: javascript html5

我试图在飞行时创建一个数据列表,并将其附加到现有的输入元素上。但是什么也没发生(没有显示下拉箭头) jQuery也可以接受。

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}

提琴: https://jsfiddle.net/tomsx/704cxako/

(取自本网站的示例: http://blogs.microsoft.co.il/gilf/2012/07/30/quick-tip-autocomplete-using-html5-datalist-element/

4 个答案:

答案 0 :(得分:1)

您必须将输入元素的list属性设置为数据列表的ID:

<input id="my-text-box" list="dlCities"/>

工作代码示例:

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}
fillDataList();
<input id="my-text-box" list="dlCities"/>

或::如果您不想修改HTML,则可以使用Element.setAttribute()在JavaScript中设置属性:

container.setAttribute('list','dlCities');

var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

function fillDataList() {
    var container = document.getElementById('my-text-box'),
    i = 0,
    len = optionList.length,
    dl = document.createElement('datalist');
    container.setAttribute('list','dlCities'); // Set the attribute here

    dl.id = 'dlCities';
    for (; i < len; i += 1) {
        var option = document.createElement('option');
        option.value = optionList[i];
        dl.appendChild(option);
    }
    container.appendChild(dl);
}
fillDataList();
<input id="my-text-box"/>

答案 1 :(得分:1)

在加载页面时仅显示输入内容:

注意如何直接使用“ input.setAttribute('list','datalist')”而不是直接使用“ input.list ='datalist'”。

var datalist = document.createElement('datalist');
datalist.id = "datalist";
document.body.appendChild(datalist);
["Seattle", "Las Vegas", "New York", "Salt lake City"].forEach(function(data) {
  var option = document.createElement('option')
  option.value = data
  datalist.appendChild(option)
});
document.querySelector('#my-text-box').setAttribute('list', "datalist");
<input id='my-text-box' />

答案 2 :(得分:0)

在更改jquery文本及其附加选项时,使用input尝试以下解决方案。

$(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<len; i++) {
					var opt = $("<option></option>").attr("value", arr[i]);
					dataList.append(opt);
				}
			}

	});
});
<!doctype html>
<html>
<head>
<title>Example 1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>

<p>
	<input type="text" name="search" id="search" placeholder="Type Something" list="searchresults" autocomplete="off" />
	<datalist id="searchresults"></datalist>
</p>

</body>
</html>

答案 3 :(得分:0)

JS

    var optionList = ["Seattle", "Las Vegas", "New York", "Salt lake City"];

    function fillDataList() {

        var container = document.getElementById('my-text-box'),
        i = 0,
        len = optionList.length,
        dl = document.createElement('datalist');

        dl.id = 'dlCities';
        for (; i < len; i += 1) {
            var option = document.createElement('option');
            option.value = optionList[i];
            dl.appendChild(option);
        }
        container.appendChild(dl);
    }

fillDataList();

HTML

<input id="my-text-box" list='dlCities'>