【发布时间】: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') }}。既然你提到了它,我试图在上面的代码中包含<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>。它有效!图书馆有什么不同吗?顺便说一句,谢谢你指出来。
标签: javascript adonis.js