2 回答
TA贡献1895条经验 获得超7个赞
显然这个问题与 Laravel 布局页面(app.blade.php)有关。它在调用 bootstrap 时已经加载了 jQuery。所以在 php 中我不需要再次加载 jQuery。
所以修订app.blade.php:
<!-- Scripts, remove defer from app.js, move selec2.min.js & select2.min.css here -->
<script src="{{ asset('js/app.js') }}"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
...
...
<!- and adding this part -->
@yield('scripts')
在 php 方面,Select2 的更改是:
在 onClick 函数之后,
select2('destroy')首先调用然后克隆包含选择元素的行
最后使用 . 在最后一个新添加的元素上重新启用 Select2
select2()。
@section('scripts')
<script>
$(document).ready(function () {
$("#contents").children("td").children("select").select2();
$(document).on('click', '#delRow', function(){
if( $(this).closest('tr').is('tr:only-child') ) {
alert('cannot delete last row');
}
else {
$(this).closest('tr').remove();
}
});
$("#addRow").click(function () {
$("#contents").children("td").children("select").select2("destroy").end();
// add <tr><td> directive first
var newRow = '<tr id="contents"><td id="trx"><td> <i class="fas fa-minus-circle" id="delRow">';
$(newRow).appendTo($("#treatmentlist"));
// append select component on the last tr
$('#treatmentlist tr:last').children("td:first-child").append(
// clone the row and insert it in the DOM
$("#contents").children("td").children("select").first().clone()
);
// enable Select2 on the last newly added element
$('#treatmentlist tr:last').children("td").children("select").select2();
// enable Select2 on the select elements
$("#contents").children("td").children("select").select2();
});
});
</script>
@endsection
在 HTML 中,应用如下更改:
<tbody id="treatmentlist">
<tr id="contents">
<td id="trx">
{{Form::select('treatments[]',$treatments, null, ['class' => 'form-control treatment', 'placeholder' => 'Select treatment' ])}}
</td>
<td>
<i class="fas fa-plus-circle" id='addRow'></td>
</td>
</tr>
</tbody>
TA贡献2037条经验 获得超6个赞
我不能评论低于 50 的声誉,希望这个例子对你有所帮助。解释在这里http://www.phpjavascript.com/2018_03_16_archive.html
$(function () {
$("#addrow").click(function () {
var tr = "<tr>" + "<td contenteditable='true'></td>".
repeat($("#myTable tr:eq(0) td").length) + "</tr>"
$("#myTable").append(tr);
});
$("#addcolumn").click(function () {
$("#myTable tr").append($("<td contenteditable='true'></td>"));
$(document).on('click', '.js-del-row', function () {
$('#myTable').find('tr:last').remove();
});
$(document).on('click', '.js-del-column', function () {
$('#myTable').find('td:last').remove();
});
});
});
- 2 回答
- 0 关注
- 123 浏览
添加回答
举报
