【问题标题】:Uncaught TypeError: Cannot read property 'init' of undefined in Javascript while submitting form未捕获的类型错误:提交表单时无法读取 Javascript 中未定义的属性“init”
【发布时间】:2019-10-14 16:15:01
【问题描述】:

我正在尝试使用 python web.py 模块制作一个社交网络迷你网络应用程序。 我用html制作了一个注册表单,但是当我填写详细信息并提交时,浏览器控制台显示以下输出:

Uncaught TypeError: $(...).ready(...) is not a function
at scripty.js:22
loaded
jquery-3.4.1.min.js:2 Uncaught TypeError: Cannot read property 'init' of undefined
at HTMLDocument.<anonymous> (scripty.js:4)
at e (jquery-3.4.1.min.js:2)
at t (jquery-3.4.1.min.js:2)

我已经搜索过类似的问题,但他们的解决方案似乎不适用于这种情况。

这是我的“scripty.js”文件代码

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

    $.material.init();

    $(document).on("submit", "#register-form", function (e) {
        e.preventDefault();
        console.log("form submitted");

        let form = $('#register-form').serialize(); //get the form data

        //send an ajax request over to the route /postregisteration
        $.ajax({
            url: '/postregisteration',
            type: 'POST',
            data: form,
            success: function (response) {
                console.log(response);
            }
        });
    });
})();

这里是python中的controller.py文件代码

import web
from Models import RegisterModel


urls = (
    '/', 'Home',
    '/register', 'Register',
    '/postregistration', 'PostRegistration'
)

render =  web.template.render("Views/Templates", base="MainLayout")

app = web.application(urls, globals())


# Classes/Routes
# Each class will be controlling a route

class Home:
    def GET(self):
        return render.Home()

class Register:
    def GET(self):
        return render.Register()

class PostRegistration:
    def POST(self):
        data = web.input()
        print(data.username)

        reg_model = RegisterModel.RegisterModel.insert_user(data)
        return data.username


if __name__=="__main__":
    app.run()

点击提交后,它只是打印 “已加载”并在浏览器控制台上显示错误。

但它也必须显示 “提交的表单”。

任何帮助都将不胜感激。

【问题讨论】:

  • 好吧,在您的代码中,您从对象$.material 调用了一个名为init 的函数,但似乎$.material 未定义。你必须看看你为什么要调用它,它是什么等等。你忘记加载那个脚本了吗?你用它之类的吗?这里的问题与 Python 无关,是您的客户端 javascript 出现故障。
  • 其实我正在上python的课程,而且我是JS的新手,所以我只是复制了这个JS代码来从html表单中获取数据。我搜索了整个项目,但没有找到“$material”。我不知道老师为什么写,我认为这是jquery的问题。有没有其他方法可以使用 web.py 从 python 中的 html 表单获取数据
  • 不要随便复制 JS 代码;最后一行写着})();,这会导致第一个错误;将其更改为});。接下来,删除$.material.init(); 行,你不需要它。
  • 我通过复制新文件/材料结构来修复它,它可以工作。

标签: javascript jquery python-3.x web.py


【解决方案1】:

更改您显示的 JS 的最后一行:

来自

})();

});

这修复了您的第一个错误... is not a function

对于第二个错误,这意味着 $.materialundefined。要么删除它(如果你没有使用材料设计)或确保相应的插件可用。

【讨论】:

    【解决方案2】:

    这是您的脚本,其中修正了结束行错误,删除了材料并添加了 cmets,解释了每一行的作用。

    // Wait until the page has fully loaded before applying the script
    $(document).ready(function () {
        // Bind a function to the submit form
        $(document).on("submit", "#register-form", function (e) {
            // Prevent the default form post behavior to prevent the page from reloading
            e.preventDefault();
    
            // Get the form data
            var form = $('#register-form').serialize(); 
    
            // Send an ajax request over to the route /postregisteration
            $.ajax({
                url: '/postregisteration',
                type: 'POST',
                data: form,
                success: function (response) {
                    console.log("form submitted");
                    console.log(response);
                }
            });
        });
    });
    

    【讨论】:

    • 他们不应该。这是一个有效的小提琴(当然“postregisteration”在那里不存在,所以ajax请求失败,但javascript代码是有效的):jsfiddle.net/insats/8bc19ap7/11
    • 嗯,现在已经修好了,谢谢,我在这里卡了一段时间了。
    猜你喜欢
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-02-26
    • 2016-01-01
    • 2012-07-04
    • 2021-01-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多