我应该将 Javascript 代码放在 ColdFusion 模型粘合视图中的什么位置?
假设我有一个名为 login.cfm
的 ColdFusion Model Glue 视图。其中,我有一个表单:
<form id="loginForm" action="#event.linkTo("user.login")#" method="POST">
E-mail: <input id="emailField" type="text" name="email">
Password: <input id="passwordField" type="text" name="password">
<input type="submit" value="Login">
</form>
现在我想在用户单击“登录”按钮时添加一些 Javascript 验证。 jQuery 中类似这样的内容:
<script type="text/javascript">
$(function() {
$('#loginForm').submit(function() {
// check that emailField is not empty and is a valid e-mail
// check that passwordField is not empty
// if validation fails, add in DOM elements to show error messages
});
});
</script>
我应该在哪里添加此 Javascript 代码?我是否将其直接粘贴到 login.cfm
视图中?或者有更好的方法来处理这个问题吗?最好,我想将所有视图中的 Javascript 代码粘贴到正文的底部。
Let's say I have a ColdFusion Model Glue view called login.cfm
. In it, I have a form:
<form id="loginForm" action="#event.linkTo("user.login")#" method="POST">
E-mail: <input id="emailField" type="text" name="email">
Password: <input id="passwordField" type="text" name="password">
<input type="submit" value="Login">
</form>
Now I want to add in some Javascript validation for when the user clicks the Login button. Something like this in jQuery:
<script type="text/javascript">
$(function() {
$('#loginForm').submit(function() {
// check that emailField is not empty and is a valid e-mail
// check that passwordField is not empty
// if validation fails, add in DOM elements to show error messages
});
});
</script>
Where should I be adding this Javascript code? Do I stick it directly into the login.cfm
view? Or is there a better way of handling this? Preferably, I'd like to stick my Javascript code from all my views to the bottom of the body.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此有很多不同的策略。最简单的方法是将视图特定的脚本放入单个 .js 文件中,并在关闭
标签。标记之前将其包含在内>
您还可以查看 require.js 等库,它们允许您异步加载脚本文件及其依赖项。
There are a quite a few different strategies for this. The easiest is it put your view-specific script in a single .js file and include it through a
<script src="path"></script>
tag just before closing the<body>
tag.You can also look at libraries such as require.js that allow you to asynchronously load script files and their dependencies.
您可以将其保留在视图文件中。
我不太喜欢使用
您可以在 login.cfm 文件中正确执行此操作。
You could just leave that in the view file the way it is.
I am not a big fan of having <script> blocks all over the place though, so I typically will use <cfsavecontent> and <cfhtmlhead> to make sure all these blocks of JS wind up in the <head>
You could do this right in the login.cfm file.