【问题标题】:json values via checkboxes通过复选框的 json 值
【发布时间】:2020-11-12 21:47:39
【问题描述】:

在 FE 我有两个搜索。一种搜索是通过文本:如果我输入邮政编码,则 json 的正确输出可以正常工作。但我想通过复选框进行第二次搜索。在那里,您应该能够按产品找到合适的公司。

但是我在 Javascript 方面还不够好,无法了解它是如何工作的。 我做了一个jsfiddle。如果你能帮忙就太好了。

您可以在下面查看我的代码,在https://jsfiddle.net/kzayjn4m/ 中以更好的方式查看

"productLines": ["Heimanwender", "SOHO", "Small Business" , "Medium Business", "Enterprise", "Öffentliche Hand", "Gesundheitswesen"],

document.getElementById('hero').addEventListener('onchange', function(e){
if(e.which == 13){
    searchStores();
}

});

function searchStores() {
var foundStores = [];
var zipCode = document.getElementById('zip-code-input').value; 
var place = document.getElementById('hero').checked;
 
 if (place) {
     stores.forEach(function (store, index) {
         var places = store.productLines;
        
            if (places == true) { 
            foundStores.push(store);
        
        }
    }
    )
}

【问题讨论】:

    标签: javascript json checkbox


    【解决方案1】:

    您可以在每个输入上监听点击事件,如果该函数已被赋予产品值,则查看商店产品数组是否包含该值

    const inputs = document.querySelectorAll('#hero input');
    
    inputs.forEach(input => {
        input.addEventListener("click", function (e) {
            if (this.checked) {
                searchStores(this.value)
            }
        });
    })
    
    function searchStores(product) {
        var foundStores = [];
        var zipCode = document.getElementById('zip-code-input').value;
    
        if (product) {
            stores.forEach(function (store) {
                if (store.productLines.includes(product)) {
                    foundStores.push(store);
                }
            })
        }
    }
    

    【讨论】:

    • 感谢您的帮助。我试图让它发生,但它不起作用。我在 html 测试和您的代码中使用了此代码。也许这是 queryselector all 中的 '#hero input' 的问题。 json中出现的所有值都必须在这里列出吗?感谢您的帮助
    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多