【问题标题】:How to filter dropdown from ajax to display checkbox with the name on it?如何从 ajax 过滤下拉菜单以显示带有名称的复选框?
【发布时间】:2021-12-29 07:32:06
【问题描述】:

如何过滤下拉列表,当我选择类型时,它会在名称复选框中显示 ajax 响应中的类型

这是html的代码

fieldset.Field {
  margin-top: 10px;
  padding: 0;
  height: 100%;
}

ul.checkbox {
  margin: 0;
  padding: 0;
  margin-left: 20px;
  list-style: none;
}

ul.checkbox li input {
  margin-right: .25em;
}

.container-body ul.checkbox li {
  float: left;
  min-width: 350px;
}
<div class="container position">
  <div class="row">
    <label for="recyclable-type" class="col-form-label"> <b>Recyclable Type:</b> </label> &nbsp;&nbsp;
    <select id="recyclable-type" name="recyclable-type" class=" custom-select col-4">
      <option selected value="">All</option>

    </select>
  </div>
</div>

<div class="container-body ">
  <fieldset class="Field">
    <ul id="checkbox" class="checkbox">
  </fieldset>
  </ul>
</div>
</div>

这是 ajax 的代码,我在其中调用我的下拉列表以及类型和名称的复选框

$.ajax({
                // The url that you're going to post
                /*

                This is the url that you're going to put to call the
                backend api,
                in this case, it's
                https://ecoexchange.dscloud.me:8090/api/get (production env)

                */
                url:"https://ecoexchange.dscloud.me:8090/api/get",
                // The HTTP method that you're planning to use
                // i.e. GET, POST, PUT, DELETE
                // In this case it's a get method, so we'll use GET
                method:"GET",
                // In this case, we are going to use headers as
                headers:{
                    // The query you're planning to call
                    // i.e. <query> can be UserGet(0), RecyclableGet(0), etc.
                    query:"RecyclableTypeGet()",
                    // Gets the apikey from the sessionStorage
                    apikey:sessionStorage.getItem("apikey")
                },
                success:function(data,textStatus,xhr) {
                    // console.log(data);
                    for (let i = 0;i<data.length;i++) {
                        $("#recyclable-type").append(
                            `
                            <option val="${data[i]["RecyclableType"]}">${data[i]["RecyclableType"]}</option>
                            `
                        );
                        // can also use this way
                        // $("#select-recyclables").append(
                        //  `
                        //  <option val="${data[i]["RecyclableType"]}">${data[i]["RecyclableType"]}</option>
                        //  `
                        // );
                    }
                },
                error:function(xhr,textStatus,err) {
                    console.log(err);
                }
            });


            $.ajax( {
            url: 'https://ecoexchange.dscloud.me:8090/api/get',
            type: 'GET',
            dataType: 'json',
            headers:{
                query: "RecyclableGet(0)",
                // Gets the apikey from the sessionStorage
                apikey: sessionStorage.getItem("apikey")
            },
            success: function(data) {
            //console.log(data);
            var html='';
            $.each(data, function(key, value) {
                html +='<input type="checkbox" name="recyclable_id[]" value="'+value.RecyclableID+'"><label style="padding-left: 10px;">'+value.Name+'</label><br>';
                //console.log(value)
            });
            $('#checkbox').html(html);

            }
        });

这就是我的 ajax 响应的样子

[
    {
        "RecyclableID": 1,
        "Name": "recyclable",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 3,
        "Name": "test recyclable 2",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 129,
        "Name": "test recyclable 4",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 131,
        "Name": "test recyclable 6",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 132,
        "Name": "test recyclable 7",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 133,
        "Name": "test recyclable 8",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 134,
        "Name": "test recyclable 34",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 138,
        "Name": "recyclable thing",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 139,
        "Name": "recyclable thing 2",
        "RecyclableType": "Ewaste"
    },
    {
        "RecyclableID": 153,
        "Name": "test recyclable 10",
        "RecyclableType": "Other"
    },
    {
        "RecyclableID": 154,
        "Name": "test recyclable 11",
        "RecyclableType": "Ewaste"
    },
    {
        "RecyclableID": 155,
        "Name": "test recyclable 123",
        "RecyclableType": "test recyclable type 2"
    },
    {
        "RecyclableID": 159,
        "Name": "some recyclable name",
        "RecyclableType": "CC"
    },
    {
        "RecyclableID": 161,
        "Name": "some recyclable name 2",
        "RecyclableType": "Ewaste"
    },
    {
        "RecyclableID": 162,
        "Name": "recyclable 2",
        "RecyclableType": "test recyclable type 2"
    },
    {
        "RecyclableID": 165,
        "Name": "test recyclable 15",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 166,
        "Name": "test recyclable 18",
        "RecyclableType": "testing type"
    },
    {
        "RecyclableID": 167,
        "Name": "some recyclable name 23",
        "RecyclableType": "Ewaster"
    },
    {
        "RecyclableID": 168,
        "Name": "Lorem ipsum dolor sit amet, consectetur",
        "RecyclableType": "test recyclable type"
    },
    {
        "RecyclableID": 169,
        "Name": "Copper",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 170,
        "Name": "Choking Bar",
        "RecyclableType": "Ewaste"
    },
    {
        "RecyclableID": 171,
        "Name": "Cabinet",
        "RecyclableType": "Other"
    },
    {
        "RecyclableID": 172,
        "Name": "Cash Box",
        "RecyclableType": "WASTE"
    },
    {
        "RecyclableID": 173,
        "Name": "Copper Telephone Cable",
        "RecyclableType": "Other"
    },
    {
        "RecyclableID": 174,
        "Name": "Rope",
        "RecyclableType": "CC"
    },
    {
        "RecyclableID": 175,
        "Name": "Silver",
        "RecyclableType": "test recyclable type"
    }
]

这是我调用 ajax 时网站示例图像的样子

这是我目前的 html 网站的样子

现在我想知道,如果选择了该类型,它会在 ajax 响应中显示它所具有的任何类型的名称,我该怎么做?

【问题讨论】:

    标签: javascript jquery ajax checkbox drop-down-menu


    【解决方案1】:

    首先,您需要在创建复选框时添加 data-type 属性。所以改变你的代码如下

    $.each(data, function(key, value) {
        var type = value.RecyclableType;
        html +='<li data-type="'+ type + '"><input type="checkbox" name="recyclable_id[]" value="'+value.RecyclableID+'"><label style="padding-left: 10px;">'+value.Name+'</label><br></li>';
    });
    

    然后在您的下拉列表中绑定一个更改事件,以便您可以过滤您的复选框 onchange

     $("#checkbox").change(function(){
            var type = $(this).value;
            // hide all checkbox
            $(this).find("li").hide();
            
            //show only checkbox having the  data-type attribute
            $(this).find("li[data-type='" + type + "']").show();      
            
    })
    

    【讨论】:

    • //仅显示具有数据类型属性的复选框 $(this).find("div[data-type='" + type + "']").show();嗨,我已经尝试过了,但是当我尝试选择它时更改事件不起作用没有显示任何内容 console.log(type) 的值确实显示了我的类型
    • @Flow 抱歉,我在选择器中使用了 div 而不是输入。我已经更新了代码。你能再试一次吗?
    • 嗨我已经改变了它仍然发生第n次没有给出错误也我已经将我的输入字段更改为
      这会影响查找输入的东西吗?
    • @Flow 我已经更新了代码:将change事件的绑定更改为#checkbox,并且我将data-type属性放在li上而不是放在input上。可以再测试一下吗?
    • 不确定你如何测试它,因为我有一个 api 令牌我可以尝试放置假数据,但我的朋友帮助我使用你的代码使代码工作,因为某些代码在调用时不正确跨度>
    • 【解决方案2】:

      我的朋友帮我找出答案

           $("#recyclable-type").change(function(){
          var type = $(this).val();
      
          //show only checkbox having the  data-type attribute
          if ($(this).val()!="" && $(this).val()!=null) {
              // hide all checkbox
              $("#checkbox").find("li").hide();
      
              // filters by recyclable type
              $("#checkbox").find("li[data-type='" + type + "']").show();
          } else {
              $("#checkbox").find("li").show();
          }
              // console.log($(this).find("li[data-type='" + type + "']"));
          })
      

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签