简体   繁体   English

如何在 ejs 文件中使用传递的变量

[英]How to use a passed variable in an ejs file

SO i am trying to change the value of an html tag in an ejs file to a variable i declared in a JavaScript file所以我试图将 ejs 文件中 html 标记的值更改为我在 JavaScript 文件中声明的变量

let checkbox = document.querySelector('input[name="plan"]');

checkbox.addEventListener('change', function () {
    if (this.checked) {
        document.querySelector('.plan-title').innerHTML = investment.name;
        document.querySelector('.plan-description').innerHTML = investment.description;
    }
    else {
        document.querySelector('.plan-title').innerHTML = '';
        document.querySelector('.plan-description').innerHTML = '';
    }
});

So when I pass it directly it shows but I want it to be dynamic and Although it gets pass through when i click the checkbox it doesn't seem to have any value.因此,当我直接通过它时,它会显示,但我希望它是动态的,尽管当我单击复选框时它会通过它似乎没有任何价值。

<%- include('../partials/sidebar'); %>
    <% if(currentUser && currentUser.isAdmin){ %>
        <a href="/investment/new">Add New Plan</a>
        <% } %>
            <div class="container">
                <h1>Investments</h1>

                <% investments.forEach((investment)=>{ %>
                    <div class="col-md-4">
                        <div class="card">
                            <strong>
                                <%= investment.name %>
                            </strong>
                            <h4>
                                <%= investment.min %> - <%=investment.max %>
                            </h4>
                            <p>
                                <%= investment.caption %>
                            </p>
                            <p><input type="checkbox" name="plan" id="">Choose investment</p>
                        </div>
                        <% if(currentUser && currentUser.isAdmin){ %>
                            <a href="/investment/<%= investment._id %>/edit">Edit</a>
                            <a href="/investment/new">Delete</a>
                            <% } %>

                    </div>

                    <% }) %>

                        <% investments.forEach((investment)=>{ %>
                            <div class="">
                                <div><strong>Package: </strong>
                                    <p class="plan-title">
                                    </p>
                                </div>
                                <p class="plan-description">

                                </p>
                                <input type="number" name="" id="" min="<%= investment.min %>"
                                    max="<%= investment.max %>">
                            </div>


                            <% }) %>


            </div>

            <%- include('../partials/footer'); %>

I cant seem to get through this, need help thanks!我似乎无法通过这个,需要帮助谢谢!

It's not clear to me what variable you're referring to, but any variable you set in a client-side script will not be available to you in an EJS file that you're rendering on the server.我不清楚您指的是什么变量,但是您在客户端脚本中设置的任何变量在您在服务器上呈现的 EJS 文件中对您不可用。 Server-side Node.js code and client-side JavaScript code have no knowledge of each other.服务器端 Node.js 代码和客户端 JavaScript 代码互不相识。

If I got it right, you are trying to insert the value of the EJS variable in the HTML tag from JavaScript when the user clicks the checkbox.如果我做对了,当用户单击复选框时,您将尝试在 JavaScript 的 HTML 标记中插入 EJS 变量的值。

The value of the HTML tag doesn't change because in your JS code: HTML 标记的值不会改变,因为在您的 JS 代码中:

    document.querySelector('.plan-title').innerHTML = investment.name;
    document.querySelector('.plan-description').innerHTML = investment.description;

investment.name and investment.description are undefined. investment.description investment.name Check the console on your page.检查页面上的控制台。

This is because you tried accessing EJS variables after the page finished rendering.这是因为您在页面完成渲染后尝试访问 EJS 变量。

EJS is mainly used to pass server-side variables to the page before it is rendered. EJS 主要用于在页面渲染之前将服务器端变量传递给页面。 So once it's rendered you cannot access those variables.因此,一旦呈现,您将无法访问这些变量。

So to have the values of those variables in your JavaScript after the page finishes rendering, try doing:因此,要在页面完成渲染后在 JavaScript 中获取这些变量的值,请尝试执行以下操作:

    document.querySelector('.plan-title').innerHTML = '<%- investment.name %>';
    document.querySelector('.plan-description').innerHTML = '<%- investment.description %>';

instead.反而。 This is how you pass the EJS variable to JavaScript.这就是将 EJS 变量传递给 JavaScript 的方式。 JavaScript now sees it as a string and there's no problem, unlike in your code where it was looking for investment object and returned undefined since that variable is not defined on the client-side. JavaScript 现在将其视为一个字符串并且没有问题,这与在您的代码中寻找investment object 并返回 undefined 不同,因为该变量未在客户端定义。

Also, since you have a for-each loop in the HTML part, I'm assuming you are trying to change the values of specific plan-title and plan-description divs.此外,由于您在 HTML 部分中有一个 for-each 循环,我假设您正在尝试更改特定plan-titleplan-description div 的值。 If that's the case, '<%= investment.name %>' and '<%= investment.description %>' in JavaScript part should be in a for-each loop as well, but that would be a lot of mess.如果是这种情况,JavaScript 部分中的 '<%=invest.name '<%= investment.name %>''<%= investment.description %>'也应该在一个 for-each 循环中,但这会很混乱。

I suggest you instead to right under the for-each loop in the HTML part, add class to the div tag according to the index of the for-each loop, add on change event to the checkbox, and pass the checkbox and the index of the for-each loop to the JavaScript function which would handle the on change event, include the EJS variables in the plan-title and plan-description divs, and in the JavaScript function that handles on change event change the CSS display property from display: none to display: block to these divs.我建议您改为在 HTML 部分中的 for-each 循环下方,根据 for-each 循环的索引将 class 添加到 div 标签,将更改事件添加到复选框,并传递复选框和索引the for-each loop to the JavaScript function which would handle the on change event, include the EJS variables in the plan-title and plan-description divs, and in the JavaScript function that handles on change event change the CSS display property from display: none display: block这些 div。

See an example:看一个例子:

HTML: HTML:

            <% investments.forEach((investment, index)=>{ %>
                <div class="col-md-4">
                    <div class="card">
                        <strong>
                            <%= investment.name %>
                        </strong>
                        <h4>
                            <%= investment.min %> - <%=investment.max %>
                        </h4>
                        <p>
                            <%= investment.caption %>
                        </p>
                        <p><input onchange="displayPlan(this, '<%= index %>')" type="checkbox" name="plan" id="">Choose investment</p>
                    </div>
                    <% if(currentUser && currentUser.isAdmin){ %>
                        <a href="/investment/<%= investment._id %>/edit">Edit</a>
                        <a href="/investment/new">Delete</a>
                        <% } %>

                </div>
                <% }) %>
                    <% investments.forEach((investment, index)=>{ %>
                        <div class="plan <%= index %>" style="display: none;">
                            <div><strong>Package: </strong>
                                <p class="plan-title">
                                 <%- investment.name  %>
                                </p>
                            </div>
                            <p class="plan-description">
                              <%- investment.description %>
                            </p>
                            <input type="number" name="" id="" min="<%= investment.min %>"
                                max="<%= investment.max %>">
                        </div>
                        <% }) %>

JavaScript: JavaScript:

    function displayPlan(checkbox, id){
        if (checkbox.checked) {
            document.querySelector(`.plan.${id}`).style.display = 'block';
        }
        else {
            document.querySelector(`.plan.${id}`).style.display = 'none';
        }
    }

Cheers!干杯!

EDIT: Grammar and syntax issues编辑:语法和语法问题

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM