在男性堆栈应用程序中使用jQuery

发布于 2025-02-07 22:02:48 字数 3587 浏览 0 评论 0原文

我正在使用mongo,express,node.js构建一个应用程序。我试图将一些jQuery嵌入一个.ejs文件中,以便用户投票帖子时,该帖子的分数会增加(或减少)。我读了这篇文章如何在Express App中使用NPM安装的NPM ?。我包括< script src =“ https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js”在我的标题中。但是,当我执行代码时,我会遇到“未定义$”的错误。这是代码的相关部分:

              <div class="carousel-inner">
                <!-- Carousel item represents each slide -->
                <div class="carousel-item active">
                  <div class="container">
                    <div class="row align-items-center">
                      <h2>
                        Use Arrow Buttons to Scroll DOWN and UP
                      </h2>
                    </div>
                  </div>
                </div>

                <%   
                  var max = questions.length
                  for (var i = 0; i < max; i++) {
                %>
                <div class="carousel-item" style="text-align: justify" >
                  <div class="row">
                    <h2><%= questions[i].title %></h2>
                    <p><%= questions[i].body %></p>
                    
                  </div>
                  <div class="row" style="width: 100%; height: 90px; white-space: nowrap; overflow-x: auto;">
                    <p style = "display: inline-block; width: 70%; height: 100%;"><strong>Posted by: </strong><%= questions[i].userid.username %> on <%= questions[i].datePosted.toDateString() %> </p>
                    <button id="submitButton" style = "display: inline-block; width: 30%; height: 50%; border-radius: 5px; background-color:darkcyan" type="submit">View Responses</button>
                  </div>
                  <div class="row" style="width: 100%; height: 90px; white-space: nowrap; overflow-x: auto;">
                    <button style = "display: inline-block; width: 25%; height: 50%; border-radius: 5px" id="minus">-</button>
                    <button style = "display: inline-block; width: 25%; height: 50%; border-radius: 5px" is="plus">+</button>
                    <%  relevance = questions[i].upvotes %>
                    <p style = "display: inline-block; width: 50%; height: 100%;" id = "score"><strong>  Score: </strong> <%= relevance %></p>
                  </div>
                  <%
                    var myBoolean = 0;
                    
                    $("#plus").click(function(){
                    
                      if (myBoolean == 0) {
                      relevance++;
                      myBoolean = 1;
                      }
                      $("#score").text(relevance);
                    });
                    
                    $("#minus").click(function(){
                      if (myBoolean == 0) {
                        relevance--;    
                      myBoolean = 1;
                      }
                      $("#score").text(relevance);
                    });
                    
                    %>
                </div>
                <% } %>
              </div>

我还想使用Mongoose更新我的MongoDB数据库,但是现在我只是在尝试获得基本的增量 /减少工作。我测试了以&lt;%var myboolean ...%的摘要以外的所有代码。%&gt;。一切都起作用。就在我尝试使用jQuery时,这是行不通的。有人可以将我指向正确的方向吗?

I am building an application using Mongo, Express, Node.js. I am trying to embed some JQuery inside an .ejs file so that when the user upvotes a post, the score for that post is incremented (or decremented). I read this post how to use jQuery installed with npm in Express app?. I have included the <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> in my header. But when I execute my code I get an error that '$ is not defined'. Here is the relevant portion of the code:

              <div class="carousel-inner">
                <!-- Carousel item represents each slide -->
                <div class="carousel-item active">
                  <div class="container">
                    <div class="row align-items-center">
                      <h2>
                        Use Arrow Buttons to Scroll DOWN and UP
                      </h2>
                    </div>
                  </div>
                </div>

                <%   
                  var max = questions.length
                  for (var i = 0; i < max; i++) {
                %>
                <div class="carousel-item" style="text-align: justify" >
                  <div class="row">
                    <h2><%= questions[i].title %></h2>
                    <p><%= questions[i].body %></p>
                    
                  </div>
                  <div class="row" style="width: 100%; height: 90px; white-space: nowrap; overflow-x: auto;">
                    <p style = "display: inline-block; width: 70%; height: 100%;"><strong>Posted by: </strong><%= questions[i].userid.username %> on <%= questions[i].datePosted.toDateString() %> </p>
                    <button id="submitButton" style = "display: inline-block; width: 30%; height: 50%; border-radius: 5px; background-color:darkcyan" type="submit">View Responses</button>
                  </div>
                  <div class="row" style="width: 100%; height: 90px; white-space: nowrap; overflow-x: auto;">
                    <button style = "display: inline-block; width: 25%; height: 50%; border-radius: 5px" id="minus">-</button>
                    <button style = "display: inline-block; width: 25%; height: 50%; border-radius: 5px" is="plus">+</button>
                    <%  relevance = questions[i].upvotes %>
                    <p style = "display: inline-block; width: 50%; height: 100%;" id = "score"><strong>  Score: </strong> <%= relevance %></p>
                  </div>
                  <%
                    var myBoolean = 0;
                    
                    $("#plus").click(function(){
                    
                      if (myBoolean == 0) {
                      relevance++;
                      myBoolean = 1;
                      }
                      $("#score").text(relevance);
                    });
                    
                    $("#minus").click(function(){
                      if (myBoolean == 0) {
                        relevance--;    
                      myBoolean = 1;
                      }
                      $("#score").text(relevance);
                    });
                    
                    %>
                </div>
                <% } %>
              </div>

I also want to update my mongoDB database with the updated score using Mongoose, but right now I am just trying to get the basic increment / decrement to work. I tested all the code OTHER THAN the snippet that starts with <% var myBoolean ... %>. It all works. Its just when I try to use the JQuery that this does not work. Can someone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文