如何在jade文件中的js文件中调用javascript函数

时间:2017-05-30 18:18:53

标签: javascript node.js mongodb express pug

我有一个名为sample.jade的文件和另一个名为insertToEnrolledSubjects.js文件的文件,(我使用的是带有快速框架的nodejs,对于db我使用的是mongo db)

sample.jade文件显示一个包含课程详细信息的表。通过单击表格的每一行中的注册,编辑,删除按钮,学生可以注册课程,编辑课程或删除它。因此,当我单击“注册”按钮时,它会显示一条弹出消息询问用户单击“确定”按钮时的确认我需要在insertToEnrolledSubjects.js文件中调用“insertToEnrolledSubjects”函数我该怎么做?请任何人都可以给我一个更好的解决方案我尝试了很多,但我没有得到任何结果。

sample.jade



 html
    head
        title="Enroll For Subjects"
        link(rel='stylesheet', href='/bootstrap/css/bootstrap.min.css')
        link(rel='stylesheet', href='/bootstrap/css/bootstrap-responsive.min.css')


    script(src='https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js')
            script(src='bootstrap/js/bootstrap.min.js')
            script(src="insertToEnrolledSubjects.js")
        body
            div.container
                div.content
                    h2.display-4.m-b-2 Course criteria 
                    table.table.table(border='2')
                        thead
                            tr
                                th Course ID
                                th Subject name
                                th Year
                                th Credits
                                th Action
                        tbody
                            - for (var i = 1; i <= 5; ++i){ 
                            tr
                                    td=i
                                    td="IPE"
                                    td=i
                                    td=5
                                    td
                                            button.btn.btn-danger(type='submit',onclick='Enroll("#{i}")') Enroll
                                    td
                                            button.btn.btn-danger(type='submit',onclick='Edit("#{i}")') Edit
                                    td
                                            button.btn.btn-danger(type='submit',onclick='delete1("#{i}")') Delete
                            - }

            script.
                function delete1(d){
                    var val = confirm("Do you want to delete this record?");
                    if(val == true){
                        window.location.href= '/delete1/'+d;
                        return true;
                    }
                    else{
                        return false;
                    }
                    };

            script.
                function Enroll(d){
                    var val = confirm("Do you really want to enroll for this course?");
                    if(val == true){
                        val2 = confirm("came");
                        insertToEnrolledSubjects(d,1);
                        val3 = confirm("came3");
                        }};




    insertToEnrolledSubjects.js
    var MongoClient = require('mongodb').MongoClient
        , format = require('util').format;
        MongoClient.connect("mongodb://localhost:27017/test", function (err, db) {

        if (err) 
            throw err;
        else {
            console.log("successfully connected to the database!!!");

             //calling insert function
            insertToEnrolledSubjects(cid,e);

            //implementation of insert function
            function insertToEnrolledSubjects(courseid,enrolled) {
            db.collection('EnrolledSubjects').insert({ courseId:courseid ,NoOfEnrolledStudents:enrolled});

            console.log("Inserted!");

            //Show total no of reults in db
            db.collection('EnrolledSubjects').count(function (err, count) {
                if (err) throw err;

                console.log("Total Rows: " + count);
            });
            }


        }});

0 个答案:

没有答案