如何将EJS代码添加到JQuery Append函数?
我有问题。我具有动态形式,并需要与数据库中的数据一起循环。
我在第一行中有一个:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了从前端执行此操作(我认为这是您要问的是因为您要问使用jQuery插入),这些步骤如下:
ejs.render()
将其传递给该模板的模板和数据,以与插入数据一起生成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:
ejs.render()
passing it the template and the data to use for that template to generate HTML with the data inserted.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.