javascript - 如何传递值以形成动作 laravel
问题描述
我有一个表格可以查看包含名称和 id 的数据,当单击编辑按钮以在同一页面中以表格形式显示此数据以进行编辑时,我想要对其进行编辑,这是在 JS 中制作的
我传递详细信息的编辑按钮
<button onclick="openForm2('{{ $t->name }}');"> edit </button>
JS函数
function openForm2($val) {
document.getElementById("name").value = $val;
}
要更新的表格
<form method="post" action="{{route('type.update',$t->id)}}"
class="form-container">
<h1>edit type</h1>
@csrf
{{ method_field('PATCH') }}
<input type="text"
name="name"
id="name"
required>
<button type="submit" class="btn">edit</button>
</form>
问题是表格的动作
action="{{route('type.update',$t->id)}}
如何传递被点击元素的id?
解决方案
如果你的路由 'type.update' 端点是 '/type/{id}' 那么 Laravel Blade 将渲染
{{route('type.update', $t->id)}} 作为' http://your-url.com/type/5 '。
如果您希望 JavaScript 设置 id,您需要更新操作并将操作设置为“ http://your-url.com/type/5 ”。
//Blade
<button onclick="openForm2('{{ $t->name }}', '{{route('type.update', $t->id)}}');"> edit </button> //Pass the route as action to JS function
<form method="post" action="{{route('type.update',$t->id)}}" class="form-container" id="my-form"> //Set id to "my-form" used by JS
//JS
function openForm2(type, action) {
document.getElementById("name").value = val;
var form = document.getElementById("my-form");
form.action = action; // Update your form with the action containing your new id used for submit
}
推荐阅读
- android - 基于 Android 和 iOS 的 Flutter 刚开始出现错误:无法加载 providerinstaller 模块
- javascript - 创建属性并使用数组元素对其进行初始化时出现类型错误
- javascript - 新创建的 div 进入 (Ii) 列表的底部,而不是进入选定的 (li) 元素?
- php - 导出所有 Twilio 可编程聊天消息
- android - 如何跳过名称中包含“单词 A”的所有任务?
- r - ifelse在R中有两个条件数字和分类
- python - 有没有办法在 Python 中运行 for 循环而不必将列表传递给它?
- lucene - 如何对 Lucene 查询的结果进行分组,按组计算命中并突出显示某些选定组中的文档?
- typescript - transpileOnly 时 ts-loader 时间超慢:假?
- react-native - 将 react-native 中的多个 html-tags 替换为 Text