单击按钮时添加到无序列表

时间:2020-04-15 02:55:59

标签: javascript jquery

我对JQuery和Im还是陌生的,我正在尝试制作此餐厅订购系统。我试图做到这一点,以便当您从食物选择中单击按钮时。它将食物的名称和价格添加到无序列表中,以便您可以看到在侧面选择的食物和价格的列表。

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>

1 个答案:

答案 0 :(得分:2)

我编辑了您的代码,现在可以正常工作了,但是 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选择器中非常重要 你写这个

$(".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>