gpt4 book ai didi

javascript - 如何找到数据列表模板选项 innerHTML 值?

转载 作者:太空宇宙 更新时间:2023-11-04 15:50:00 25 4
gpt4 key购买 nike

我想知道如何限制 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值(value)?

之前 添加 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 现在在输入期间如何受到限制,但在提交时我不再获得 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);
}
<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>

最佳答案

因为你已经保存了模板,你可以用它来搜索匹配的选项:

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>

关于javascript - 如何找到数据列表模板选项 innerHTML 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332627/

25 4 0
文章推荐: javascript - 如何将