简体   繁体   English

从 Laravel 控制器将值传递给 ajax,然后获取特定字段

[英]From laravel controller passing value to ajax then get the particular field

how can i get the ID or Name Field then append it to Textbox, Please Correct me if i'm approaching this in the wrong way.如何获取 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'));

image below is the result if i use console.log(students)下图是如果我使用console.log(students) 在此处输入图片说明

Updated Version更新后的版本

$(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)
});
});

Attached imaged result:附上图片结果: 在此处输入图片说明

Try this one.试试这个。 it will append with value.它将附加值。

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

You should iterate over a students array like so:你应该像这样迭代一个学生数组:

students.forEach(function(student) {
   console.log(student.name);
   console.log(student.id);
});

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

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