繁体   English   中英

如何传递价值形成行动 laravel

[英]how to pass value to form action laravel

我有一个表来查看包含名称和 ID 的数据,当我单击编辑按钮以在同一页面的表单中显示此数据以进行编辑时,我想要它,这是在 JS 中制作的

我传递详细信息的编辑按钮

<button onclick="openForm2('{{ $t->name }}');"> edit </button>

JS function

    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?

我有一个表格来查看包含我想要的名称和 ID 的数据,当单击编辑按钮以在同一页面中以表格形式显示此数据以进行编辑时,这是在 JS 中制作的

我传递详细信息的编辑按钮

<button onclick="openForm2('{{ $t->name }}');"> edit </button>

JS function

    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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM