首页 > 解决方案 > 单击按钮时添加到无序列表

问题描述

我还是 JQuery 的新手,我正在尝试制作这个餐厅订购系统。我试图做到这一点,以便当您单击食物选择中的按钮时。它将食物的名称和价格添加到无序列表中,这样您就可以在旁边看到您选择的食物和价格的列表。

var coll = $(".collapsible");

for (var i = 0; i < coll.length; i++) {
    
  coll[i].addEventListener("click", function() {
    
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}


$(".button.table").click(function(){
    var price = $(this).attr("id");
    var name = $(this).attr("name");
    $(".orders li").html("<li>" + price + "</li>");
});
/* Splits Screen */
html, body { 
    height: 100%; 
    padding: 0; 
    margin: 0; 
}

/* Choice section */
.choice { 
    width: 70%; 
    height: 100%; 
    float: left;
    padding: 0 25px 0 25px;
    background-color: gray;
}


/* Order List */
.order { 
    width: 30%; 
    height: 100%; 
    float: left;
}

h1{
    font-size: 50px;
    font-weight: 700;
    color: black;
    text-align: center;
}

#appetizer{
   width: 100%; 
}
#entree{
   width: 100%; 
}
#dessert{
   width: 100%; 
}
#drinks{
   width: 100%; 
}


.collapsible {
    background-color: lightgray;
    border-radius: 5px;
    border: 2px solid black;
    margin: 5px;
    color: black;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    text-align: center;
    outline: none;
    font-size: 30px;

  }
  
  .content {
    padding: 0;
    display: none;
    overflow: hidden;
    background-color: white;
  }

  .food{
      margin: auto;
      width: 100%;
      height: 55px;
      font-size: 20px;
  }

  ul li{
      list-style: none;
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <!-- BootStrap -->
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
     <link rel="stylesheet" href="styles2.css">

    <title>Order</title>
</head>
<body>
    <div class="choice">
            <h1>Choices</h1>
            
               <button class="collapsible">Appetizers</button>
                <div class="content">
                    <button class="food" name="Wings" id="8">Wings</button>
                    <button class="food" name="Calamari" id="12">Calamari</button>
                    <button class="food" name="Cheese Dip" id="7">Cheese Dip</button>
                </div>

               <button class="collapsible">Entrees</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>

               <button class="collapsible">Desserts</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>
                
               <button class="collapsible">Drinks</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>
    </div>


    <div class="orderList">
            <h1>Order:</h1>
            <div class="list-group">
                <ul class= "orders">
                    <li></li>
                </ul>
            </div>
    </div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="tables.js"></script>
</body>
</html>

标签: javascriptjquery

解决方案


我编辑你的代码,现在可以正常工作但是 1-ID 不是一个数字,你可以使用数据-获取信息,比如价格 2-Jquery 有很短的命令,如果你写 JQUERY 而不是纯 javascript,你可以使用它,例如你写这个

for (var i = 0; i < coll.length; i++) {

  coll[i].addEventListener("click", function() {

    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });
}

但我写这个

  $(".collapsible").click(function(){

    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });

3-在Jquery Selector中你写这个很重要

$(".button.table")

你没有button上课也没有表课这个选择器什么都返回

    
 $(".collapsible").click(function(){
    
    this.classList.toggle("active");
    var content = this.nextElementSibling;
    if (content.style.display === "block") {
      content.style.display = "none";
    } else {
      content.style.display = "block";
    }
  });



$("button.food").click(function(){
    var price = $(this).attr("data-price");
    var name = $(this).attr("name");
    $(".orders").append("<li>" +name+":"+ price + "</li>");
});
/* Splits Screen */
html, body { 
    height: 100%; 
    padding: 0; 
    margin: 0; 
}

/* Choice section */
.choice { 
    width: 70%; 
    height: 100%; 
    float: left;
    padding: 0 25px 0 25px;
    background-color: gray;
}


/* Order List */
.order { 
    width: 30%; 
    height: 100%; 
    float: left;
}

h1{
    font-size: 50px;
    font-weight: 700;
    color: black;
    text-align: center;
}

#appetizer{
   width: 100%; 
}
#entree{
   width: 100%; 
}
#dessert{
   width: 100%; 
}
#drinks{
   width: 100%; 
}


.collapsible {
    background-color: lightgray;
    border-radius: 5px;
    border: 2px solid black;
    margin: 5px;
    color: black;
    cursor: pointer;
    padding: 18px;
    width: 100%;
    text-align: center;
    outline: none;
    font-size: 30px;

  }
  
  .content {
    padding: 0;
    display: none;
    overflow: hidden;
    background-color: white;
  }

  .food{
      margin: auto;
      width: 100%;
      height: 55px;
      font-size: 20px;
  }

  ul li{
      list-style: none;
  }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <!-- BootStrap -->
     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
     <link rel="stylesheet" href="styles2.css">

    <title>Order</title>
</head>
<body>
    <div class="choice">
            <h1>Choices</h1>
            
               <button class="collapsible">Appetizers</button>
                <div class="content">
                    <button class="food" name="Wings" data-price="8">Wings</button>
                    <button class="food" name="Calamari" data-price="12">Calamari</button>
                    <button class="food" name="Cheese Dip" data-price="7">Cheese Dip</button>
                </div>

               <button class="collapsible">Entrees</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>

               <button class="collapsible">Desserts</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>
                
               <button class="collapsible">Drinks</button>
                <div class="content">
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                    <button class="food">Wings</button>
                </div>
    </div>


    <div class="orderList">
            <h1>Order:</h1>
            <div class="list-group">
                <ul class= "orders">
                 
                </ul>
            </div>
    </div>



<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="tables.js"></script>
</body>
</html>


推荐阅读