在Node JS中的按钮单击上的EJS之间传递变量

时间:2020-05-03 15:40:27

标签: javascript node.js express ejs

我正在国家公园的Node JS中创建一个项目,从数据库中获取公园列表并将其显示在我的主页上。现在,在显示的各种公园列表中,我必须单击View Page按钮将那个公园的名称的值传递到应用程序的下一页。我该如何在Node JS中做到这一点

这是我的home.ejs文件

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link href="https://fonts.googleapis.com/css?family=Pacifico&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="../css/national-parks.css">


  </head>
  <body>


        <div class="container" id="headContainer">
          <div class="row">
            <div class="col-2">
              <nav class="nav flex-column">
                <a class="nav-link active text-white" href="/login">Login</a>
                <a class="nav-link active text-white" href="/signup">Sign Up</a>
                <a class="nav-link active text-white" href="/logout">Logout</a>
              </nav>
            </div>

            <div class="col-10" id="heading">
              <h1 class="display-3">National Parks Observer</h1>
              <p class="lead">Site Dedicated to Indian National Parks</p>
             </div>


         </div>
        </div>




        <div class="container" id="postings">

         <%  var r=Math.round(nationalParks.length/3); %>
         <% var x = 1; %>
         <% var elementCount = 3; %>
         <% var constant = 3; %>
         <% var start=0; %>
         <% while(x<=r){ %>
          <div class="row">

           <% for(var k=start;k<elementCount;k++){ %>

            <div class="col-4">
              <div class="card">
                <img class="card-img-top" src=<%=nationalParks[k].imageurl %> alt="Card image cap">
                <div class="card-body">
                  <h5 class="card-title"><%=nationalParks[k].nationalPark %></h5>
                  <a href="/NationalPark" class="btn btn-primary btn-sm" id="viewNationalPark">View Page</a>
                </div>
              </div>
           </div>

           <% } %>
           <%  start=start+constant; %>
           <%   elementCount=Math.min(elementCount+constant,nationalParks.length); %>


          </div>

          <% x++; }  %>


        </div>


        <br>
        <br>
        <br>



    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
    <script src="../resources/javascript/national-park.js"></script>     
  </body>
</html> 

点击以下按钮代码

<div class="card-body">
                  <h5 class="card-title"><%=nationalParks[k].nationalPark %></h5>
                  <a href="/NationalPark" class="btn btn-primary btn-sm" id="viewNationalPark">View Page</a>
                </div>

我需要将<%=nationalParks[k].nationalPark %>的值传递到应用程序的下一页,是否可以在Node JS中做到这一点。提前致谢。

0 个答案:

没有答案