【问题标题】:Passing variable from ejs to js script将变量从 ejs 传递到 js 脚本
【发布时间】:2019-01-20 11:57:49
【问题描述】:

我正在尝试将一个变量从 ejs 传递给 JavaScript 函数,但在输出中一直未定义。这是代码。它应该呈现带有文件名的表格,然后单击每个文件名应该稍后重定向到将在浏览器中呈现文件的页面,但如果不将参数传递给 URL,我将无法继续。

        <% if (files.length > 0) {%>
        <table class="table table-hovered">
            <thead class="thead-light">
                <tr>
                    <th scope="col">No</th>
                    <th scope="col">File</th>
                    <% files.forEach((file, index) => { %>
                <tr>
                    <th scope="row">
                        <%= index + 1 %>
                    </th>
                    <td>
                        <a onclick="func(file.fileName);">
                            <%= file.fileName %>
                        </a>
                    </td>
                </tr>
                <% }) %>
                </tbody>
        </table>
        <% } %>
    </div>
</div>
<script>
    function func(fileName) {
        window.location.href = "/thesis_view/" + fileName;
    }
</script>




getStudentUploadThesisPage: (req, res) => {
        const getFilesQuery = "SELECT fileName FROM supervision INNER JOIN thesis ON supervision.thesisId = thesis.id INNER JOIN thesis_details ON thesis_details.thesisId = thesis.Id INNER JOIN people ON supervision.teacherId = people.id INNER JOIN thesis_file ON thesis_details.id = thesis_file.thesis_detail_id WHERE supervision.studId = " + req.session.userId;
        db.query(getFilesQuery, (err, result) => {
            if (err) {
                return res.status(500).send(err);
            } else if (result.length >= 0) {
                res.render('student_upload_thesis', {
                    files: result
                });
            }
        });
    }

【问题讨论】:

  • 你能说明你是如何将数据传递给视图的吗?

标签: javascript node.js ejs


【解决方案1】:

我想出了类似的东西。它可以工作

<script>
    var table = document.getElementById('table'),
        rIndex;
    for (var i = 0; i < table.rows.length; i++) {
        table.rows[i].onclick = function () {
            rIndex = this.rowIndex;
            window.location.href = "/thesis_view/" + this.cells[1].innerHTML;
        }
    }
</script>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    • 2014-10-27
    • 2017-04-06
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多