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

时间:2018-07-13 20:49:55

标签: javascript html5 html-datalist html-templates

我想知道如何限制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);
}

1 个答案:

答案 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>