Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在jQuery中编写ejs变量_Javascript_Jquery_Node.js_Ejs - Fatal编程技术网

Javascript 在jQuery中编写ejs变量

Javascript 在jQuery中编写ejs变量,javascript,jquery,node.js,ejs,Javascript,Jquery,Node.js,Ejs,我使用jQuery将一些html附加到div: public/js/writeInputs.js $("#myDiv").append("<input type='search' value='<%= filters.title %>' name='filters[title]' class='form-control'>") <input type="search" name="filters[title]" value="<%= filters.titl

我使用jQuery将一些html附加到
div

public/js/writeInputs.js

$("#myDiv").append("<input type='search' value='<%= filters.title %>' name='filters[title]' class='form-control'>")
<input type="search" name="filters[title]" value="<%= filters.title %>" class="form-control">
<script>
    var ejsVal = <%= val %>; // You have to use var to make it global
</script>
<script src="script.js"></script>
console.log(ejsVal);
如果我将此值硬编码到html中,它将按预期工作:

视图/部分/输入。ejs

$("#myDiv").append("<input type='search' value='<%= filters.title %>' name='filters[title]' class='form-control'>")
<input type="search" name="filters[title]" value="<%= filters.title %>" class="form-control">
<script>
    var ejsVal = <%= val %>; // You have to use var to make it global
</script>
<script src="script.js"></script>
console.log(ejsVal);

如何在jQuery中编写ejs变量

我在append中尝试过这些方法,但都不起作用:

value="<%= filters.title %>"
value=${<%= filters.title %>}
value='"<%= filters.title %>"'
value=“”
值=${}
值=“”“
你能这样试试吗

var temp = <%= test  %>;
$("#myDiv").append("<input type='search' value='"+temp+"' name='filters[title]' class='form-control'>");
var-temp=;
$(“#myDiv”)。追加(“”);

Node express首先呈现HTML,然后浏览器基于HTML执行一些请求以获取脚本、图像等。因此,EJS不会解释

作为替代方案,将一些数据呈现为HTML并重写JavaScript以从HTML读取
过滤器.title
,如下所示:

<input type="hidden" id="filter" value="<%= filters.title %>" />

<script src="yourScript.js"></script>

yourScript.js

    var filter = document.getElementById("filter").value;
    $("#myDiv").append("<input type='search' value='" + filter + "' name='filters[title]' class='form-control'>");
var filter=document.getElementById(“filter”).value;
$(“#myDiv”)。追加(“”);

服务器仅呈现视图文件夹中的ejs文件,因此您必须在该文件中创建变量。以下是一个例子:

view.ejs

$("#myDiv").append("<input type='search' value='<%= filters.title %>' name='filters[title]' class='form-control'>")
<input type="search" name="filters[title]" value="<%= filters.title %>" class="form-control">
<script>
    var ejsVal = <%= val %>; // You have to use var to make it global
</script>
<script src="script.js"></script>
console.log(ejsVal);

您的代码是在ejs文件中还是在常规js文件中?
append
发生在
js
文件中,但是它出现的元素是一个
ejs
模板。您是否记得确保
writeInputs.js
不会从您的public dir中作为静态资产使用,而是具有自己的get routing函数,以便您的服务器可以加载文件并使用
respond.render(…)
?请查看。您可以在浏览器中使用ejs值声明变量,该变量将可用于jquery。您正在使用服务器使用
ejs
语法呈现页面。任何具有ejs语法的文件都需要以这种方式提供,用户不能将它们放在static/public目录中并直接从该文件加载。
writeInputs.js
的URL请求需要转到实际的服务路径(如
app.get('writeInputs.js',…)
,如果您使用的是express),并且如果您希望替换您的ejs语法,则必须使用ejs呈现。谢谢,但它仍然呈现为字符串文本:
“”
@fugu changed你能试试这个吗,但是
在我的
.js
文件中不可用,或者将筛选值放在那里,你必须在引号内写下你的ejs语法,如“”