【问题标题】:Can Javascript be written in .edge file?Javascript可以写在.edge文件中吗?
【发布时间】:2019-01-07 02:55:52
【问题描述】:

我刚开始学习 Adonis.js,我想知道是否可以在 .edge 文件中编写 Javascript?我发现本教程 (here) 关于如何动态添加表格行,我想在我的 Adonis.js 项目中实现它。我已经在<script> 标签内添加了编码,但是当我点击“添加”按钮时没有任何反应。有谁知道如何解决这个问题?或者甚至对此有其他解决方案?

提前感谢您的帮助。

@layout('main')

@section('content')
    <a href="/">Go back</a>
    <hr>

    <h1>Create Club</h1>

    <form action="/clubs/create" method="POST">

        {{ csrfField() }}

        <div class="form-group">
            <label>Club Name</label>
            <input type="text" name="clubName" class="form-control" value="{{ old('clubName', '') }}">
            {{ elIf('<span class="text-danger">$self</span>', getErrorFor('clubName'), hasErrorFor('clubName')) }}
        </div>

        <div class="form-group">
                <label>Club Description</label>
                <textarea name="clubDesc" class="form-control">{{ old('clubDesc', '') }}</textarea>
                {{ elIf('<span class="text-danger">$self</span>', getErrorFor('clubDesc'), hasErrorFor('clubDesc')) }}
        </div>

        <br>

        <table class="table order-list">
            <thead>
                <tr>
                    <td>Name</td>
                    <td>Position</td>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>
                        <input type="text" name="memberName" class="form-control"/>
                    </td>
                    <td>
                            <input type="text" name="position" class="form-control"/>
                    </td>
                </tr>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="5" style="text-align: left;">
                        <input type="button" class="btn btn-sm btn-block" id="addrow" value="Add Member"/>
                    </td>
                </tr>
            </tfoot>
        </table>

        <button class="btn btn-primary" type="submit">Create!</button>

    </form>

    <script>
        $(document).ready(function () {
            var counter = 0;

            $("#addrow").on("click", function () {
                var newRow = $("<tr>");
                var cols = "";

                cols += '<td><input type="text" class="form-control" name="memberName' + counter + '"/></td>';
                cols += '<td><input type="text" class="form-control" name="position' + counter + '"/></td>';

                cols += '<td><input type="button" class="ibtnDel btn btn-md btn-danger "  value="Delete"></td>';
                newRow.append(cols);
                $("table.order-list").append(newRow);
                counter++;
            });

            $("table.order-list").on("click", ".ibtnDel", function (event) {
                $(this).closest("tr").remove();       
                counter -= 1
            });

        });

        function calculateRow(row) {
            var price = +row.find('input[name^="price"]').val();
        }

        function calculateGrandTotal() {
            var grandTotal = 0;
            $("table.order-list").find('input[name^="price"]').each(function () {
                grandTotal += +$(this).val();
            });
            $("#grandtotal").text(grandTotal.toFixed(2));
        }
    </script>

@endsection

【问题讨论】:

  • 是的,您可以在您的 .edge 文件中添加 javascript 代码
  • 关于“ADD BUTTON”的问题,能否分享一下代码?
  • @Pepe 我已经在问题中添加了代码
  • @Pepe 但是,我对给出的教程中的“价格”变量感到好奇。你知道它在代码中代表什么吗?
  • @ChristopherDosin 我在我的main.edge 文件中添加了{{ script('https://code.jquery.com/jquery-3.3.1.slim.min.js') }}。既然你提到了它,我试图在上面的代码中包含&lt;script src="//code.jquery.com/jquery-1.11.1.min.js"&gt;&lt;/script&gt;。它有效!图书馆有什么不同吗?顺便说一句,谢谢你指出来。

标签: javascript adonis.js


【解决方案1】:

来自docs

要编写原始 HTML,您必须将其包装在 {{{ }}}

这表明你应该能够做到*:

{{{
<script>
   // .. JavaScript here
</script>
}}}

*可能还有其他可能更好的方法。我以前从未使用过模板引擎,但上面似乎可以完成您正在寻找的内容。

【讨论】:

  • 我试过用代码包装{{{ }}},它似乎像这样{{{}}}出现在我的页面底部,并且它仍然可以在没有用@包装代码的情况下工作987654325@。无论如何,我已经发现了问题所在。它是图书馆。我之前包含的那个似乎不适用于代码。不过谢谢你的回复,我很感激。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多