使用 laravel 用 Select 框填充许多文本框
Filled many Textbox with Select box using laravel
我想根据 selected 选项在文本框中显示数据。例如,如果我 select 书名,它将显示书的作者、书的价格、书的印刷年份。
我想根据选择的 selected 选项
显示图书
我的控制器
use Illuminate\Http\Request;
use App\Models\P_Peminjaman;
use App\Models\M_Asset;
use App\Models\M_Perpustakaan;
use App\Models\User;
class P_PeminjamanController extends Controller
{
public function getpengarang_buku(Request $request)
{
$kode_buku = $request->kode_buku;
$perpustakaan = Regency::where('buku_kode', $kode_buku)->get();
foreach($perpustakaan as $buku){
echo "<input id='$buku->pengarang_buku' type='text' class='$buku->pengarang_buku'>";
echo "<input id='$buku->penerbit_buku' type='text' class='$buku->penerbit_buku'>";
}
}
}
我的模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class M_Perpustakaan extends Model
{
use HasFactory;
protected $table = "m_perpustakaan";
}
我的javascript
$(function()
{
$('#judul_buku').on('change', function(){
let kode_buku = $('#judul_buku').val();
// console.log(kode_buku);
$.ajax({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type : 'POST',
url : "{{ route('getpengarang_buku') }}",
data : {kode_buku:kode_buku},
cache : false,
success: function(msg){
$('#pengarang_buku').html(msg);
$('#penerbit_buku').html(msg);
}
})
})
});
我的web.php
Route::post('/getpengarang_buku',[P_PeminjamanController::class, 'getpengarang_buku'])->name('getpengarang_buku');
我的create.blade.php
<head><meta name="csrf-token" content="{{ csrf_token() }}" /></head>
<body>
<select class="form-control" name="judul_buku" id="judul_buku">
<option value="">- Pilih Buku -</option>
@foreach ($buku as $item)
<option value="{{ $item->kode_buku }}">{{ $item->judul_buku }}</option>
@endforeach
</select>
<input type="text" name="pengarang_buku" class="form-control" placeholder="Info pengarang_buku" id="pengarang_buku" value="{{ old('pengarang_buku') }}">
<input type="text" name="penerbit_buku" class="form-control" placeholder="Info penerbit_buku" id="penerbit_buku" value="{{ old('penerbit_buku') }}">
</body>
但是在文本框中根本找不到结果,谁能帮帮我?谢谢
你忘记在你的控制器上添加 return,它更好地 return 你请求的值 json 像这样而不是 echo value
public function getpengarang_buku(Request $request)
{
$kode_buku = $request->kode_buku;
$perpustakaan = Regency::where('buku_kode', $kode_buku)->get();
$data = [];
foreach($perpustakaan as $buku){
$data = ['pengarang_buku' => $buku->pengarang_buku,
'penerbit_buku' => $buku->penerbit_buku];
}
return response()->json($data);
}
并在 ajax 响应中将值传递给输入文本
$(function()
{
$('#judul_buku').on('change', function(){
let kode_buku = $('#judul_buku').val();
// console.log(kode_buku);
$.ajax({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type : 'POST',
url : "{{ route('getpengarang_buku') }}",
data : {kode_buku:kode_buku},
cache : false,
success: function(msg){
$('#pengarang_buku').val(msg['pengarang_buku']);
$('#penerbit_buku').val(msg['penerbit_buku']
}
})
})
});
我想根据 selected 选项在文本框中显示数据。例如,如果我 select 书名,它将显示书的作者、书的价格、书的印刷年份。 我想根据选择的 selected 选项
显示图书我的控制器
use Illuminate\Http\Request;
use App\Models\P_Peminjaman;
use App\Models\M_Asset;
use App\Models\M_Perpustakaan;
use App\Models\User;
class P_PeminjamanController extends Controller
{
public function getpengarang_buku(Request $request)
{
$kode_buku = $request->kode_buku;
$perpustakaan = Regency::where('buku_kode', $kode_buku)->get();
foreach($perpustakaan as $buku){
echo "<input id='$buku->pengarang_buku' type='text' class='$buku->pengarang_buku'>";
echo "<input id='$buku->penerbit_buku' type='text' class='$buku->penerbit_buku'>";
}
}
}
我的模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class M_Perpustakaan extends Model
{
use HasFactory;
protected $table = "m_perpustakaan";
}
我的javascript
$(function()
{
$('#judul_buku').on('change', function(){
let kode_buku = $('#judul_buku').val();
// console.log(kode_buku);
$.ajax({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type : 'POST',
url : "{{ route('getpengarang_buku') }}",
data : {kode_buku:kode_buku},
cache : false,
success: function(msg){
$('#pengarang_buku').html(msg);
$('#penerbit_buku').html(msg);
}
})
})
});
我的web.php
Route::post('/getpengarang_buku',[P_PeminjamanController::class, 'getpengarang_buku'])->name('getpengarang_buku');
我的create.blade.php
<head><meta name="csrf-token" content="{{ csrf_token() }}" /></head>
<body>
<select class="form-control" name="judul_buku" id="judul_buku">
<option value="">- Pilih Buku -</option>
@foreach ($buku as $item)
<option value="{{ $item->kode_buku }}">{{ $item->judul_buku }}</option>
@endforeach
</select>
<input type="text" name="pengarang_buku" class="form-control" placeholder="Info pengarang_buku" id="pengarang_buku" value="{{ old('pengarang_buku') }}">
<input type="text" name="penerbit_buku" class="form-control" placeholder="Info penerbit_buku" id="penerbit_buku" value="{{ old('penerbit_buku') }}">
</body>
但是在文本框中根本找不到结果,谁能帮帮我?谢谢
你忘记在你的控制器上添加 return,它更好地 return 你请求的值 json 像这样而不是 echo value
public function getpengarang_buku(Request $request)
{
$kode_buku = $request->kode_buku;
$perpustakaan = Regency::where('buku_kode', $kode_buku)->get();
$data = [];
foreach($perpustakaan as $buku){
$data = ['pengarang_buku' => $buku->pengarang_buku,
'penerbit_buku' => $buku->penerbit_buku];
}
return response()->json($data);
}
并在 ajax 响应中将值传递给输入文本
$(function()
{
$('#judul_buku').on('change', function(){
let kode_buku = $('#judul_buku').val();
// console.log(kode_buku);
$.ajax({
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
type : 'POST',
url : "{{ route('getpengarang_buku') }}",
data : {kode_buku:kode_buku},
cache : false,
success: function(msg){
$('#pengarang_buku').val(msg['pengarang_buku']);
$('#penerbit_buku').val(msg['penerbit_buku']
}
})
})
});