Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/419.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否让复选框显示某些内容?_Javascript_Jquery_Css_Html - Fatal编程技术网

Javascript 是否让复选框显示某些内容?

Javascript 是否让复选框显示某些内容?,javascript,jquery,css,html,Javascript,Jquery,Css,Html,我完全不知道怎么做,也不知道从哪里开始。不过,我还是想使用jQuery 我想要一份大约300件物品的清单。每个项目从一个类别分配到几个不同类别 例如: 项目: 香蕉, 橘子, 奶酪, 蛋糕 类别 水果, 黄色的, 布朗, 橙色 甜点 项目将按如下方式分配: 香蕉-黄色,水果 橘子-橘子,水果 奶酪黄 蛋糕-棕色,甜点 我需要能够勾选任何复选框组合-例如,如果我勾选“黄色和水果”,它将显示橙子、香蕉和奶酪。(注意,我需要所有的黄色加上所有的水果,而不仅仅是黄色的水果)。或者如果我只勾选黄色,它会

我完全不知道怎么做,也不知道从哪里开始。不过,我还是想使用jQuery

我想要一份大约300件物品的清单。每个项目从一个类别分配到几个不同类别

例如:

项目:

香蕉, 橘子, 奶酪, 蛋糕

类别

水果, 黄色的, 布朗, 橙色 甜点

项目将按如下方式分配:

香蕉-黄色,水果

橘子-橘子,水果

奶酪黄

蛋糕-棕色,甜点

我需要能够勾选任何复选框组合-例如,如果我勾选“黄色和水果”,它将显示橙子、香蕉和奶酪。(注意,我需要所有的黄色加上所有的水果,而不仅仅是黄色的水果)。或者如果我只勾选黄色,它会显示奶酪和香蕉。如果我在甜点和橘子上打勾,就会看到蛋糕和橘子。希望你能理解我的意思


我非常感谢您对如何实现这一点的任何见解或帮助。

我将创建一个对象数组,其中每个对象都有一个
名称
属性和一个
类别
属性,该属性是一个字符串数组,其中每个字符串都是对象所属的类别名称。然后,您可以检查选中了哪些类别复选框,然后在所有对象中循环,仅显示在其“类别”属性中包含所有选定类别的对象。可以通过创建具有选定对象特性的循环元素来显示这些对象。使用
for
循环中的
for
循环可以找到正确的对象,如下所示:

伪代码:

//loop through the array of objects
//    for ever element, loop through its `categories` property and check if
//    it has the current element in the array of selected categories 
//    (`selected`). Then, do that for every element in `selected`

我知道这可能会让人困惑,如果是的话,我很抱歉,但我希望这会有所帮助。

我会制作一个对象数组,其中每个对象都有一个
名称
属性和一个
类别
属性,该属性是一个字符串数组,其中每个字符串都是对象所属的类别名称。然后,您可以检查选中了哪些类别复选框,然后在所有对象中循环,仅显示在其“类别”属性中包含所有选定类别的对象。可以通过创建具有选定对象特性的循环元素来显示这些对象。使用
for
循环中的
for
循环可以找到正确的对象,如下所示:

伪代码:

//loop through the array of objects
//    for ever element, loop through its `categories` property and check if
//    it has the current element in the array of selected categories 
//    (`selected`). Then, do that for every element in `selected`

我知道这可能会让人困惑,如果是的话,我很抱歉,但我希望这会有所帮助。

Matt B。由于您是web开发新手,让我告诉您一个解决方案,根据我的经验,我认为它可能最适合您,现在就来。您是web开发新手,所以我建议您现在不要使用MySql作为数据库,而使用PHP作为逻辑基础。。。因此,我认为你现在必须关注HTML、CSS和JS,让我来指导你如何实现这一目标,要点是。。。 1.创建一个结构如下的html文件

<input class="yFruit" type="checkbox" value="YellowFruit"> Yellow Fruit<br>
<input class="rFruit" type="checkbox" value="RedFruit"> Red Fruit<br>
<div class="itemsContainer">
    <div class="item" data-category="YellowFruit"> Banana </div>
    <div class="item" data-category="RedFruit"> Apple </div>
</div>
黄色水果
红色水果
香蕉 苹果
  • 使用Css根据您的喜好设置这些元素的样式

  • 现在是Javascript的时候了

  • 您知道javascripts事件在这种情况下非常有用,比如当我点击某个东西时,我可以检测到我点击的elemenet是什么。同样,当我们选择一个我们用html创建的复选框时,我们可以看到它与所有元素匹配的值(在本例中,所有的水果都带有class=“item”),并且我们可以向每个项目添加css属性,就像我们想隐藏一些元素一样,我们现在也可以这样做,让我们看看代码

    // JS Code
    // assigning all the checkbox in variables
    var yFruit = document.querySelector(".yFruit");
    var rFruit = document.querySelector(".rFruit");
    // allItems is an array containing all the item elements in it
    var allItems= document.querySelectorAll(".item");
    
    // creating a function to only show the yFruit
     function showYFruitOnly(){
        for (i = 0; i < allItems.length; i++) {
            if(allItems[i].getAttribute("data-category") == "YellowFruit"){
                // display only if it is from yellow fruit category
                allItem[i].style.display = "block";
            }else{
                // do not display if it is not from that category  
                allItem[i].style.display = "none";
            }
        }
     }
    
    // code below this line is checking if yFruit is checked or not 
    //every time some one clicks on it
    // if it is checked then the code is running
    yFruit.onchange = function(){
    if(yFruit.checked == true){
        // running the showYFruitOnly function if checkbox is checked
        showYFruitOnly;
    }
    }
    
    //JS代码
    //在变量中指定所有复选框
    var yFruit=document.querySelector(“.yFruit”);
    var rFruit=document.querySelector(“.rFruit”);
    //allItems是一个数组,包含其中的所有item元素
    var allItems=document.queryselectoral(“.item”);
    //创建只显示yFruit的函数
    函数showYFruitOnly(){
    对于(i=0;i

    你可以为更多的案例创建更多的功能等等,试试这个,我打赌你会学到很多这样做的东西….

    Matt B。因为你是web开发新手,让我告诉你一个解决方案,根据我的经验,我认为它可能最适合你。您是web开发新手,所以我建议您现在不要使用MySql作为数据库,而使用PHP作为逻辑基础。。。因此,我认为你现在必须关注HTML、CSS和JS,让我来指导你如何实现这一目标,要点是。。。 1.创建一个结构如下的html文件

    <input class="yFruit" type="checkbox" value="YellowFruit"> Yellow Fruit<br>
    <input class="rFruit" type="checkbox" value="RedFruit"> Red Fruit<br>
    <div class="itemsContainer">
        <div class="item" data-category="YellowFruit"> Banana </div>
        <div class="item" data-category="RedFruit"> Apple </div>
    </div>
    
    黄色水果
    红色水果
    香蕉 苹果
  • 使用Css根据您的喜好设置这些元素的样式

  • 现在是Javascript的时候了

  • 您知道javascripts事件在这种情况下非常有用,比如当我点击某个东西时,我可以检测到我点击的elemenet是什么。同样,当我们选择一个我们用html创建的复选框时,我们可以看到它与所有元素匹配的值(在本例中,所有的水果都带有class=“item”),并且我们可以向每个项目添加css属性,就像我们想隐藏一些元素一样,我们现在也可以这样做,让我们看看代码

    // JS Code
    // assigning all the checkbox in variables
    var yFruit = document.querySelector(".yFruit");
    var rFruit = document.querySelector(".rFruit");
    // allItems is an array containing all the item elements in it
    var allItems= document.querySelectorAll(".item");
    
    // creating a function to only show the yFruit
     function showYFruitOnly(){
        for (i = 0; i < allItems.length; i++) {
            if(allItems[i].getAttribute("data-category") == "YellowFruit"){
                // display only if it is from yellow fruit category
                allItem[i].style.display = "block";
            }else{
                // do not display if it is not from that category  
                allItem[i].style.display = "none";
            }
        }
     }
    
    // code below this line is checking if yFruit is checked or not 
    //every time some one clicks on it
    // if it is checked then the code is running
    yFruit.onchange = function(){
    if(yFruit.checked == true){
        // running the showYFruitOnly function if checkbox is checked
        showYFruitOnly;
    }
    }
    
    //JS代码
    //在变量中指定所有复选框
    var yFruit=document.querySelector(“.yFruit”);
    变种果实=