简体   繁体   English

如何制作一个按钮,单击该按钮会显示 node.js 中的数据?

[英]How do I make a button that, when clicked, displays data in node.js?

So I've created a button, along with a table that shows data from my database.所以我创建了一个按钮,以及一个显示来自我的数据库的数据的表格。 However, I want that data to appear only when the button is clicked.但是,我希望该数据仅在单击按钮时出现。 So when the button is clicked, I want a random row from a specific table I have in my database appear.因此,当单击按钮时,我希望出现数据库中特定表中的随机行。 在此处输入图像描述

`<button id="breakfast2">Breakfast</button>
 <table id="mealTableContainer2">
            <thead>
                <tr>
                    <th id="mealname">Meal Name</th>
                    <th id="mealcalories">Calories</th>
                    <th id="mealtype">Type of Meal</th>
                    
                </tr>
            </thead>

            <tbody id="mealContainer2"></tbody>
  </table>`

The only Javascript I currently have is for getting my database into that table, but I really only want it to appear when I click the breakfast button.我目前唯一的 Javascript 是为了将我的数据库放入该表中,但我真的只希望它在我单击早餐按钮时出现。 I just can't figure out how;我只是想不通怎么办; if someone could explain it to me very simply, I'd be appreciate it a ton!如果有人能非常简单地向我解释,我将不胜感激!

console.log('Data2 received')
$.ajax({
    url: 'http://localhost:5000' + "/read-recordsrandom",
    type: 'get',
    success: function(response) {
        var data2 = JSON.parse(response);
        if(data2.msg === "SUCCESS"){
            console.log(data2.meal)
            createMealTable2(data2.meal);
        } else {
            console.log(data2.msg);
        }
    },
    error: function(err){
        alert(err);
    }

});

} }

function createMealTable2(data2) { 
var tableHTML2 = " ";
console.log("Test");
for(var i=0; i < data2.length; i++){
    tableHTML2 += "<tr>";
    tableHTML2 += "<td>" + data2[i].mealname + "</td>";
    tableHTML2 += "<td>" + data2[i].mealcalories + "</td>"
    tableHTML2 += "<td>" + data2[i].mealtype + "</td>";
    
    
    tableHTML2 += "</tr>";
}

$('#mealContainer2').html(tableHTML2);   

} }

Hide it initially then display it先隐藏再显示

 document.getElementById("breakfast2").addEventListener('click', function(event) { document.getElementById("mealTableContainer2").classList.remove('hidden'); });
 .hidden { display: none; }
 <button id="breakfast2">Breakfast</button> <table id="mealTableContainer2" class="hidden"> <thead> <tr> <th id="mealname">Meal Name</th> <th id="mealcalories">Calories</th> <th id="mealtype">Type of Meal</th> </tr> </thead> <tbody id="mealContainer2"></tbody> </table>

If I'm right, by default, the table is supposed to be hide.如果我是对的,默认情况下,该表应该是隐藏的。 So you can first of all, hide the table with css所以你可以首先用 css 隐藏表格

Change this line to hide the table更改此行以隐藏表格

<table id="mealTableContainer2" style="display: none;">

After that, you will need to add event listener on the button "breakfast" in javascript to display the table on click之后,您需要在javascript中的“早餐”按钮上添加事件监听器,以在点击时显示表格

document.getElementById("breakfast2").addEventListener("click", function() {
    document.getElementById("mealTableContainer2").style.display = "block";
});

Here is how you can ajax on click这是点击 ajax 的方法

NOTE: the `` are backticks used in template literal delimites and MUST be backticks注意:`` 是模板文字分隔符中使用的反引号,必须是反引号

$(function() {
  const createMealTable2 = data => {
    $('#mealContainer2')
      .html(data.map(meal => `<tr>
          <td>${meal.mealname}</td>
          <td>${meal.mealcalories}</td>
          <td>${meal.mealtype}</td>
        </tr>`)
        .join("")  
      )
  };

  $("#breakfast2").on("click", function() {
    $.ajax({
      url: 'http://localhost:5000' + "/read-recordsrandom",
      type: 'get',
      success: function(response) {
        var data2 = JSON.parse(response);
        if (data2.msg === "SUCCESS") {
          createMealTable2(data2.meal);
        } else {
          console.log(data2.msg);
        }
      },
      error: function(err) {
        alert(err);
      }
    });
  })
})
<button type="button" id="breakfast2">Breakfast</button>
<table id="mealTableContainer2">
  <thead>
    <tr>
      <th id="mealname">Meal Name</th>
      <th id="mealcalories">Calories</th>
      <th id="mealtype">Type of Meal</th>
    </tr>
  </thead>
  <tbody id="mealContainer2"></tbody>
</table>

You need an event listener that listen for a click and than do the call to your database with asynchronous function. After the answer is returned just put it on the page using createElement or inner text of already existing html element.您需要一个事件侦听器来侦听点击,而不是使用异步 function 调用您的数据库。返回答案后,只需使用 createElement 或已存在的 html 元素的内部文本将其放在页面上。

我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何制作一个表单,点击后提交到 node.js 快递服务器 - how do i make a form that when clicked submit to node.js express server 在node.js中单击时,如何使这些链接下载文件? - How to make these links to download files when clicked in node.js? 如何<a>在 node.js 中</a>获取点击<a>链接</a>的名称<a>?</a> - How do I get the name of a clicked <a> link in node.js? 我怎么做<div>单击按钮时部分不可见(JS)?</div><div id="text_translate"><p> 如何使用 JS 隐藏&lt;div&gt;部分和</p><pre>&lt;div class="alertB1"&gt; &lt;div class="sticky"&gt; &lt;h1&gt; This site is still being built&lt;/h1&gt; &lt;p&gt; Please remember this site is still being built. Click &lt;a href="form1.html"&gt;here to report a bug &lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;style&gt;.sticky { position: -webkit-sticky; position: sticky; }.alertB1 { width: 100%; height: 125px; background: lightgreen; } &lt;/style&gt;</pre><p> 所以我希望包含一个按钮:</p><pre> &lt;span id="a33"&gt;&lt;button&gt;&amp;times;&lt;/button&gt;&lt;/span&gt; &lt;/div&gt; &lt;/div&gt;</pre><p> 如何让&lt;span&gt;隐藏&lt;div&gt;标签? 谢谢,环形游戏</p></div> - How do i make a <div> section invisible when a button is clicked (JS)? 如何使用Node.JS向Ghostbin发出POST请求? - How Do I Make a POST Request with Node.JS to Ghostbin? 如何使此Node.js程序异步? - How do I make this Node.js program asynchronous? 如何在node.js中使用xml文件? - How do I make use of an xml file in node.js? 当在本机反应中单击按钮时如何运行 node.js 服务器 - how to run node.js server when button clicked in react native 我该如何做 <div> 单击按钮时滑入视图? - How Do I make a <div> slide into view when a button is clicked? 单击禁用按钮时如何显示模式? - How do I make a modal appear when the disabled button is clicked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM