我想知道如何限制datalist options
下拉菜单中显示的datalist
的数量。
我发现this solution使用datalist
环绕template
并加上一些JavaScript。
但是,我要返回与所选innerHTML
相对应的datalist option
。因此,如果我选择option value = "California"
,我想返回>"CA"<
。
document.getElementById("optionInnerHTML").innerHTML = [].find.call(document.getElementById('searchresults').getElementsByTagName('option'), ({ value }) => value === a).innerHTML;
我尝试将document.getElementById('resultstemplate')
添加到上面的代码中,但是没有任何效果。我需要更改什么才能使函数返回option innerHTML
值?
之前添加datalist options
限制(请注意,显示的下拉选项尚未受到限制,但是我成功获得了option innerHTML
的值:
/* listen for ENTER click to submit datalist option input */
function loadDoc() {
var n = document.getElementById("search");
n.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("myButton").click();
}
});
/* find search value */
var n = document.getElementById("search");
var a = n.value;
document.getElementById("searchValue").innerHTML = a;
/* find datalist option innerHTML */
document.getElementById("optionInnerHTML").innerHTML = [].find.call(document.getElementById('searchresults').getElementsByTagName('option'), ({ value }) => value === a).innerHTML;
}
<datalist id="searchresults">
<option value="Alabama">AL</option>
<option value="California">CA</option>
<option value="Florida">FL</option>
<option value="Michigan">MI</option>
<option value="New York">NY</option>
<option value="Wisconsin">WI</option>
</datalist>
<input type="text" name="search" id="search" placeholder="type state name or code" list="searchresults" autocomplete="off" />
<button id="myButton" type="button"
onclick="loadDoc()">Submit
</button>
<p>search value: <span id="searchValue"></span></p>
<p>datalist option innerHTML: <span id="optionInnerHTML"></span></p>
添加了<template>
和datalist options
的限制后(请注意,现在显示的options
在输入过程中受到了限制,但是提交后,我不再得到{ {1}}值):
option innerHTML
/* Limit length of datalist options dropdown */
var search = document.querySelector('#search');
var results = document.querySelector('#searchresults');
var templateContent = document.querySelector('#resultstemplate').content;
search.addEventListener('keyup', function handler(event) {
while (results.children.length) results.removeChild(results.firstChild);
var inputVal = new RegExp(search.value.trim(), 'i');
var clonedOptions = templateContent.cloneNode(true);
var set = Array.prototype.reduce.call(clonedOptions.children, function searchFilter(frag, el) {
if ((inputVal.test(el.textContent) || inputVal.test(el.value)) && (search.value != el.value) && frag.children.length < 5) frag.appendChild(el);
return frag;
}, document.createDocumentFragment());
results.appendChild(set);
});
/* listen for ENTER click to submit datalist option input */
function loadDoc() {
var n = document.getElementById("search");
n.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("myButton").click();
}
});
/* find search value */
var n = document.getElementById("search");
var a = n.value;
document.getElementById("searchValue").innerHTML = a;
/* find datalist option innerHTML */
document.getElementById("optionInnerHTML").innerHTML = [].find.call(document.getElementById('searchresults').getElementsByTagName('option'), ({ value }) => value === a);
}
答案 0 :(得分:3)
由于已保存模板,因此可以使用它来搜索匹配选项:
const options = templateContent.querySelectorAll('option');
const matchingOption = [].find.call(options, ({ value }) => value === a);
document.getElementById("optionInnerHTML").innerHTML = matchingOption.innerHTML;
完整的示例:
/* Limit length of datalist options dropdown */
var search = document.querySelector('#search');
var results = document.querySelector('#searchresults');
var templateContent = document.querySelector('#resultstemplate').content;
search.addEventListener('keyup', function handler(event) {
if (event.keyCode === 13) {
return;
}
while (results.children.length) results.removeChild(results.firstChild);
var inputVal = new RegExp(search.value.trim(), 'i');
var clonedOptions = templateContent.cloneNode(true);
var set = Array.prototype.reduce.call(clonedOptions.children, function searchFilter(frag, el) {
if ((inputVal.test(el.textContent) || inputVal.test(el.value)) && (search.value != el.value) && frag.children.length < 5) frag.appendChild(el);
return frag;
}, document.createDocumentFragment());
results.appendChild(set);
});
/* listen for ENTER click to submit datalist option input */
function loadDoc() {
var n = document.getElementById("search");
n.addEventListener("keyup", function(event) {
event.preventDefault();
if (event.keyCode === 13) {
document.getElementById("myButton").click();
}
});
/* find search value */
var n = document.getElementById("search");
var a = n.value;
document.getElementById("searchValue").innerHTML = a;
/* find datalist option innerHTML */
const matchingOption = [].find.call(templateContent.querySelectorAll('option'), ({ value }) => value === a);
document.getElementById("optionInnerHTML").innerHTML = matchingOption.innerHTML;
}
<datalist id="searchresults">
<template id="resultstemplate">
<option value="Alabama">AL</option>
<option value="California">CA</option>
<option value="Florida">FL</option>
<option value="Michigan">MI</option>
<option value="New York">NY</option>
<option value="Wisconsin">WI</option>
</template>
</datalist>
<input type="text" name="search" id="search" placeholder="type state name or code" list="searchresults" autocomplete="off" />
<button id="myButton" type="button"
onclick="loadDoc()">Submit
</button>
<p>search value: <span id="searchValue"></span></p>
<p>datalist option innerHTML: <span id="optionInnerHTML"></span></p>