javascript - 从 laravel Controller 将值传递给 ajax 然后获取特定字段

标签 javascript laravel

我如何获取 ID 或名称字段,然后将其附加到文本框,如果我以错误的方式处理此问题,请纠正我。

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};

    //This Part Im Trying to get.
    var name=$(students).val(name);
    var id=$(students).val(id);

    console.log(name);

});

Controller :

 $students=Student::all()->take(5);
 return view('Examples.levels',compact('students'));

下图是我使用 console.log(students) 的结果 enter image description here

更新版本

$(document).ready(function(){
    var students = {!! json_encode($students->toArray()) !!};
    students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value=`+student.name+`>`
  );
  console.log(student.name)
});
});

附加图像结果: enter image description here

最佳答案

试试这个。它将附加值。

students.forEach(function(student) {
  $('.content').append(
  `<input name="name[]" value="`+student.name+`">`
  );
});

关于javascript - 从 laravel Controller 将值传递给 ajax 然后获取特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60035302/

相关文章:

php - Laravel 5 使用 Ajax 发送密码重置链接

php - Laravel - 无法通过 View 传递数据

javascript - KnockoutJS 多个数据绑定(bind) - 值和 javascript 函数

javascript - 选择选项不与 ng-model 绑定(bind)

javascript - 如何访问没有 ID 的 HTML 元素?

php - Laravel 自定义模型类 - 字段没有默认值

javascript - 如何使用 ChartJs 改变圆环图的厚度?

javascript - 在页面加载期间使用 Leaflet 预加载图层

php - Laravel Eloquent 预加载假定数据透视表列名称不正确

php - Laravel 护照 : registered date claims is deprecated