jquery - 带有验证和存储 Jquery 的 Laravel 表单添加 html 元素
问题描述
我使用 Laravel,并且我有一个带有 laravel 验证和必填字段的表单(所以当我收到错误时,它总是重新加载所有页面,并且我使用旧刀片标签保留值)。一切都好,但我做了一个按钮来添加一些带有 jquery 的文本框
$(".add-more").click(function(){
var html = $(".copy").html();
$(".after-add-more").before(html);
});
$("body").on("click",".remove",function(){
$(this).parents(".control-group").remove();
});
当 Laravel 在提交后的每条无效消息处重新加载所有页面时,如何保存旧值和创建的 n-html 元素?谢谢
解决方案
它很简单,你不需要重新加载整个页面,例如用 laravel 的 @include 包含一个元素,表格的内容在另一个刀片文件中,但它被包含了
<table class="table tablesorter ">
<thead class=" text-primary">
<tr>
<th scope="col">ID</th>
<th scope="col">TIPO</th>
<th scope="col">fecha</th>
<th scope="col">Monto</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody id="table">
@include('component-table')
</tbody>
</table>
组件表.blade.php
<tr>
<td>hi</td>
<td>
test
</td>
<td></td>
<td></td>
<td class="text-right">
</td>
</tr>
现在,您可以创建一个返回此视图的控制器,并在您调用该控制器时仅重新加载表,并将 html 替换为 jquery 的 html() 函数
//on controller
public function table()
{
return view('component-table.blade.php');
}
//js
async function reloadTable(){
await $.ajax({
type: "GET",
url: "{{ route('component-table') }}",
data: '',
success: function(response){
$('#table').html(response)
}
});
}
推荐阅读
- angular - Sharepoint Online web api在使用角度登录后返回401
- html - 将一条长线换成一个
使线条等长
- java - 带有 SQL Server 的 Spring Data JPA 给出错误:无效的对象名称'Table_Name'
- tableau-api - Tableau - 与上一个计数的百分比差异。月
- firebase - 直接从移动应用程序或通过后端服务器访问 Firestore?
- c# - 如何在不改变其位置的情况下向特定楼梯组添加更多楼梯?
- python - 如何在 python 中驱动研华 CAN 板?
- microprocessors - 不使用任何操作系统的英特尔伽利略板
- node.js - 将简单日期(2018 年 11 月 26 日)转换为节点 js 中的日期类型
- spring - spring boot 关系未初始化