简体   繁体   English

如何从.ejs文件中获取值并将其用于.js文件

[英]how to get value from .ejs file and use it ni a .js file

I have a login.ejs file I want to use the username entered in the textbox in a .js file. 我有一个login.ejs文件我想使用在.js文件的文本框中输入的用户名。

login.ejs contain following HTML login.ejs包含以下HTML

<h1>Log in</h1>
<div>
<label>Username:</label>
<input type="text" id="username" name="uname" /><br/>
</div>
<div>
<label>Password:</label>
<input type="password" id="password" name="pw" />
</div>
<div>
<input type="submit" value="Submit"/>
</div>

and the code in .js file where I want to use that value is 以及我想要使用该值的.js文件中的代码

if(username==teacher)
{
//some thing
}
else if(username==student)
{
//some thing
}

You can put this in the head of your ejs file, before loading the a.js file 在加载a.js文件之前,您可以将它放在ejs文件的head

 "<script > var username<%= title %> </script>"

This will define the variable username with the parameter passed to the ejs file, and you would be able to access it in any js file linked to the page 这将使用传递给ejs文件的参数定义变量username,您可以在链接到该页面的任何js文件中访问它

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

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