简体   繁体   English

从 laravel balde 获取 foreach 循环的值并将其传递给 javascript 函数

[英]get the value of a foreach loop from laravel balde and pass it to javascript function

i have this For-each loop in my Laravel blade engine and i'm trying to pass the id and name to my Javascript function using button click event and log it to console,我的 Laravel 刀片引擎中有这个 For-each 循环,我正在尝试使用按钮单击事件将 ID 和名称传递给我的 Javascript 函数并将其记录到控制台,

Logic: attached image as example (1.) once button ADD is click the Student from Point A will go to Point B (2.) once button DELETE click the student from Point A will be remove from Point B and back to Point A逻辑:附加图像作为示例(1.)一旦单击 ADD 按钮,A 点的学生将转到 B 点(2.)单击 DELETE 按钮后,A 点的学生将从 B 点移除并返回 A 点在此处输入图片说明

Now what i have tried so far is logging the student name and id to the console using console.log, but problem is only the ID of the First student will come out.现在我到目前为止所尝试的是使用 console.log 将学生姓名和 id 记录到控制台,但问题是只有第一个学生的 ID 会出来。 Example if i Click Student 3 = the id is 1 it is not getting the real ID of the student.例如,如果我单击学生 3 = id 为 1,则不会获得学生的真实 ID。 how do i approach this and what is the best suggestion to achieve my goal我如何解决这个问题以及实现我的目标的最佳建议是什么

CODES代码

@foreach($students as $std)
        <input id="myInput"  type="button" value="{{$std->name}}" onclick="addRow()">
        @endforeach

function addRow() {
    var inputVal = document.getElementById("myInput").value;
    console.log(inputVal);  
}

Add data attributes to you input field and get them on onclick().将数据属性添加到您的输入字段并在 onclick() 上获取它们。 Here is data attribute documentation .这是数据属性文档

Code using Jquery使用 Jquery 编写代码

@foreach($students as $std)
    <input id="myInput"  type="button" data-name="{{$std->name}}" data-id="{{$std->id}}" value="{{$std->name}}" onclick="addRow(this)">
@endforeach

function addRow(ele) 
{
    var name= $(ele).attr('data-name');
    var id= $(ele).attr('data-id');
    console.log(name);  
    console.log(id);  
}

1st remove id from loop id must be unique第一次从循环 id 中删除 id 必须是唯一的

2nd you can pass as function parameter第二,您可以作为函数参数传递

@foreach($students as $std)
        <input   type="button" value="{{$std->name}}" onclick="addRow({{$std->name}})">
        @endforeach

function addRow(inputVal) {
    console.log(inputVal);  
}

on more solution but it need jQuery更多解决方案,但它需要jQuery

@foreach($students as $std)
        <input   type="button" value="{{$std->name}}" onclick="addRow(this)">
        @endforeach

function addRow() {
    var inputVal  = $(this).value;
    console.log(inputVal);  
}

Repeated Ids in the markup is the problem.标记中的重复 Id 是问题所在。 If the Ids are no needed at all, remove the attr Id and inside of the function addRow.如果根本不需要 Id,则删除 attr Id 和函数 addRow 内部。

Embrace the function addEventListener拥抱函数addEventListener

The following approach selects the inputs using the attribute name and then binds a click event.以下方法使用属性名称选择输入,然后绑定单击事件。

As you can see, inside of the handler, we can access the value of the input as follow: var inputVal = this.value;如您所见,在处理程序内部,我们可以按如下方式访问输入值: var inputVal = this.value;

 Array.from(document.querySelectorAll("[name='myInput']")).forEach(i => { i.addEventListener("click", function(e) { var inputVal = this.value; console.log(inputVal); }); });
 <input name="myInput" type="button" value="{{$std->name1}}"> <input name="myInput" type="button" value="{{$std->name2}}"> <input name="myInput" type="button" value="{{$std->name3}}">

The value of this inside the handler is a reference to the element.处理程序中的this值是对元素的引用。

You can only have one ID per element, but you can indeed have more than one class.每个元素只能有一个 ID,但确实可以有多个类。

Your problem is that you are using the same Id for all of your button.您的问题是您对所有按钮使用相同的 ID。 Next, is you are calling your dom element inside of your addRow function which Js can't find clearly if what dom value you want to get.接下来,您是否在 addRow 函数中调用 dom 元素,如果您想获得什么dom value ,Js 无法清楚地找到该元素。 To solve this your code should look like this.要解决此问题,您的代码应如下所示。

Pure JS纯JS

@foreach($students as $std)
        <input  class="myInput"  type="button" value="{{$std->name}}" onclick="addRow({{$std->name}})">
@endforeach

function addRow(inputVal) {
    console.log(id);  
}

JQUERY查询

// html

@foreach($students as $std)
    <input  class="myInput"  type="button" value="{{$std->name}}">
@endforeach



// your script

$('.myInput').click(function() {
     console.log(this.val())
});

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

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