繁体   English   中英

按另一个列表框的值过滤javascript中列表框的值

[英]Filtering values of a listbox in javascript by value of another listbox

我想保留selectbox1的值(URL)过滤时什么是selectbox1通过选择selectbox0值显示。 它正在针对selectbox2进行过滤。

我遇到的问题是它不是不保留 URL(值),只有内部文本在我过滤后保留在selectbox1 中

我想解决的另一件事是,如果可能的话,我想让selectbox2对用户隐藏。

<html>
<head>
    <title>Links</title>
    <select id="selectbox0" name="" ;>
        <option value="" selected>All</option>
        <option value="Google">Google</option>
        <option value="Microsoft">Microsoft</option>
        <option value="Apple">Apple</option>
    </select>
    <select id="selectbox1" name="" ;>
        <option value="https://www.google.com" selected>Google</option>
        <option value="https://www.microsoft.com">Microsoft</option>
        <option value="https://www.apple.com">Apple</option>
    </select>
        <select id="selectbox2" name="" ;>
        <option value="https://www.google.com" selected>Google</option>
        <option value="https://www.microsoft.com">Microsoft</option>
        <option value="https://www.apple.com">Apple</option>
    </select>
    <button onclick="openInTabSelect();">Open</button>
    <script type="text/javascript">
        function openInTabSelect() {
            var pSelect = document.getElementById('selectbox1').value;
            //console.log(pSelect)
            var newTab = safari.self.browserWindow.openTab();
            newTab.url = pSelect;
        }

        var selectbox0 = document.getElementById('selectbox0'),
        selectbox1 = document.getElementById('selectbox1'),
        selectbox2 = document.getElementById('selectbox2');

        selectbox0.addEventListener('change', function() {
            selectbox1.innerHTML = '';
            for (var childIndex = 0; childIndex < selectbox2.children.length; childIndex++) {
                var child = selectbox2.children[childIndex];
                if (child.innerHTML.search(selectbox0.value) != -1) {
                    option = document.createElement('option');
                    option.innerHTML = child.innerHTML;
                    selectbox1.appendChild(option);
                }
            }
        });

    </script>
</head>
</html>

预期:在selectbox0 中选择Google应该过滤掉并且只在selectbox1 中显示Google并保留其值www.google.com将用于通过单击按钮打开在新选项卡中打开

经验。 元素: < option value="https://www.google.com" selected="">Google< /option >

实际:在selectbox0中选择Google会过滤掉并且只在selectbox1 中显示Google但不保留其值www.google.com

行为。 元素: < option >Google< /option >

我相信我发现了你的问题。 您正在使用innerHTML来获取值,但您想要该值。 innerHTML获取选项字段中的文本,而不是值。 尝试这个:

if (child.search(selectbox1.value) != -1) {
   option = document.createElement('option');
   option.innerHTML = child.innerHTML;
   selectbox1.appendChild(option);
   }

您的.getElementById()语句指向一个不存在的 id selectbox

var pSelect = document.getElementById('selectbox').value;

我不太确定您打算指向哪个 ID,但请先尝试清除它。

感谢大家阅读本文,但我自己设法弄明白了。 我很抱歉问这个问题有点过早。 通过添加第二行,我首先定义了选项的值,它起作用了。

            option.value = child.value;
            option.innerHTML = child.innerHTML;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM