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

标签 javascript jquery

我对 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>

最佳答案

我编辑了你的代码,现在工作正常了但是 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>

关于javascript - 单击按钮时添加到无序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61220588/

相关文章:

javascript - 更改 JSON 对象数组的顺序

javascript - :hover selector doesn't work with jQuery 1. 4

javascript - 如何设置 jQuery 可拖动最小/最大左和最小/最大右

jquery - 关闭 Bootstrap 模式

javascript - 如何使用 JQuery 将点击功能分配给多个按钮?

javascript - 将此 js 代码从内联更改为外部

javascript - knockout 可观察不是一个函数

javascript - Jasmine 测试在测试运行中、Firefox/Chrome 之间以及检查器打开/关闭时的结果不一致

javascript - 减去页脚和页眉高度的全列高度

javascript - jQuery 删除/克隆