如何将EJS代码添加到JQuery Append函数?

发布于 2025-02-03 05:00:04 字数 1152 浏览 2 评论 0原文

我有问题。我具有动态形式,并需要与数据库中的数据一起循环。

我在第一行中有一个:

 <select class="form-select" id="serviceName1" onchange="getServiceInfo('1')" name="serviceName">
                        <option selected value="none">Wybierz pozycję</option>
                        <% invServices.forEach(invServices => { %>
                          <option><%= invServices.services.name %></option>
                        <% }); %>
                      </select>

然后复制:

$(wrapper).append(
                 "<select id='serviceName"+x+"' class='form-select' name='serviceName'>"+
                    "<option selected value='none'>Wybierz pozycję</option>"+
                     '<% invServices.forEach(invServices => { %>'+
                        "<option><%= invServices.services.name %></option>"+
                        "<% }); %>'"+
                    "</select>"+    )

此复制part&lt;%= blah blah blah%&gt;,在我的页面上像字符串一样渲染。

我可以使用此标签&lt; %%&gt;用jQuery附加?还是可能有人有更好的方法来做动态形式?

感谢您的回答。

I have problem. I have dynamic form, with and I need to looping with data from database.

I have this on first row:

 <select class="form-select" id="serviceName1" onchange="getServiceInfo('1')" name="serviceName">
                        <option selected value="none">Wybierz pozycję</option>
                        <% invServices.forEach(invServices => { %>
                          <option><%= invServices.services.name %></option>
                        <% }); %>
                      </select>

And this to copy:

$(wrapper).append(
                 "<select id='serviceName"+x+"' class='form-select' name='serviceName'>"+
                    "<option selected value='none'>Wybierz pozycję</option>"+
                     '<% invServices.forEach(invServices => { %>'+
                        "<option><%= invServices.services.name %></option>"+
                        "<% }); %>'"+
                    "</select>"+    )

And this copy part <%= blah blah blah%>, rendering like string on my page.

Can i using this tags <%%> with jquery append? Or mayby someone have any better way to do dynamic forms?

Thanks for answer.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灯角 2025-02-10 05:00:04

为了从前端执行此操作(我认为这是您要问的是因为您要问使用jQuery插入),这些步骤如下:

  1. 创建一个迷你EJS模板(看起来您已经拥有)。
  2. 从数据库中获取数据(如果您在网页中还没有数据,则可能在后端服务器上调用AJAX调用)。
  3. 导入EJS客户端库,因此可以使用客户端。
  4. 然后,调用ejs ejs.render()将其传递给该模板的模板和数据,以与插入数据一起生成HTML。
  5. 然后,使用jQuery将生成HTML生成的html插入您想要的现有页面。

或者,您可以创建一个单个AJAX调用,该调用在服务器上执行步骤1-4并为您返回生成的HTML,然后插入生成的HTML,AJAX调用将返回您想要的位置。

To do this from the front-end (which I assume is what you're asking because you're asking about inserting with jQuery), the steps are as follows:

  1. Create a mini EJS template (which it looks like you already have).
  2. Get the data from the database (probably with an Ajax call to your back-end server if you don't already have the data in the web page).
  3. Import the EJS client-side library so it is available for client use.
  4. Then, call EJS ejs.render() passing it the template and the data to use for that template to generate HTML with the data inserted.
  5. Then, use jQuery to insert that generated HTML into your existing page where you want it.

Alternately, you could create a single Ajax call that does steps 1-4 on the server and returns generated HTML for you and then you insert that generated HTML that the Ajax call returns into your page where you want it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文