【问题标题】:Web.py trouble using JS fileWeb.py 使用 JS 文件的问题
【发布时间】:2017-11-30 01:23:30
【问题描述】:

我正在关注 web.py 框架的教程,我基本上是在为博客设置框架。但是,我在实现注册表单的时候,使用的是网课老师提供给我的一个js文件。如下:

$(document).ready(function(){
console.log("loaded");
$.material.init();

$(document).on("submit", "#register-form", function(e){
    e.preventDefault();
    console.log("form submitted");
    var form = $('#register-form').serialize();
    $.ajax({
        url: '/postregistration'
        type: 'POST'
        data: form
        success: function(response){
            console.log(response);
        }
    });
});

});

在 HTML 文件中,我将其定义如下,以及我的其余 js 文件:

$var js: static/js/jquery-3.2.1.min.js static/js/bootstrap.min.js static/js/material.min.js static/js/ripples.min.js static/js/scripty.js

scripty.js 位于 static/js 中,所以我相信我的语法没问题,但是当我通过 localhost 访问网站时,我没有从 Chrome 中的控制台得到任何反馈。这是一个问题,因为我想使用 AJAX 来获取信息,但我无法判断 js 是否正常工作。谁能帮我找出问题所在?谢谢!

【问题讨论】:

    标签: javascript python web.py


    【解决方案1】:

    $var js 只是定义了一个名为js 的参数。它实际上并没有告诉浏览器加载 javascript——事实上,它本身并没有告诉浏览器任何事情。

    您需要在模板中编写适当的 HTML:

    <script src="/static/js/jquery...."></script>
    <script src="/static/js/bootst...."></script>
    

    等等

    如果您想使用您的 $js,您可以让您的模板为您编写负载:

    $if content.get('js', None):
        $for x in content.js.split(' '):
            <script src="/$x"></script>
    

    【讨论】: