我应该将 Javascript 代码放在 ColdFusion 模型粘合视图中的什么位置?

发布于 2024-12-02 15:19:02 字数 935 浏览 0 评论 0原文

假设我有一个名为 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 技术交流群。

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

发布评论

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

评论(2

椵侞 2024-12-09 15:19:02

为此有很多不同的策略。最简单的方法是将视图特定的脚本放入单个 .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.

不可一世的女人 2024-12-09 15:19:02

您可以将其保留在视图文件中。

我不太喜欢使用

您可以在 login.cfm 文件中正确执行此操作。

<cfsavecontent variable="js">
<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>
</cfsavecontent>
<cfhtmlhead text="#js#" />

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.

<cfsavecontent variable="js">
<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>
</cfsavecontent>
<cfhtmlhead text="#js#" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文