[英]How to pass dropdown value to controller using ajax jquery
I need to pass a value from a dropdown list to a controller using ajax.我需要使用 ajax 将下拉列表中的值传递给 controller。 Here's my code:
这是我的代码:
<div class="dropdown">
<input class="btn btn-primary dropdown-toggle" id="btnDropDownCabang" name="btnDropDownCabang"
value="Pilih Salah Satu" type="button"
data-bs-toggle="dropdown" aria-expanded="false" style="width:250px">
Pilih Salah Satu
<ul class="dropdown-menu mh" style="max-height: 250px; overflow-y: auto;" id="dropDownMenu"
aria-labelledby="dropdownMenuButton1" style="width:250px;">
@foreach($cabang['data'] as $listCabang)
<li><a class="dropdown-item" href="#">{{$listCabang->cab_kodecabang}}
- {{$listCabang->cab_namacabang}}</a></li>
@endforeach
</ul>
</div>
$.ajax({
type: 'POST',
url: 'monitoring_plano/public/laporan/test',
data: {
'btnDropDownCabang': kodeCabang,
},
success: function (Response) {
alert(Response)
}
})
public function test(Request $request)
{
$kodeCabang = $request->request->get('btnDropDownCabang');
dd($kodeCabang);
}
When I run it, it returns null.当我运行它时,它返回 null。 Can anyone help me?
谁能帮我? Thank you
谢谢
in dropdown-item you can create as below在下拉项目中,您可以创建如下
<a class="dropdown-item" href="#" data-kode="{{ $listCabang->cab_kodecabang }}">
and then in javascript like this.然后像这样在 javascript 中。
$('body').on('click', '.dropdown-item', function() {
let kodeCabang = $(this).attr('data-kode');
$.ajax({
type: 'POST',
url: 'monitoring_plano/public/laporan/test',
data: {
'btnDropDownCabang': kodeCabang,
},
success: function (Response) {
alert(Response)
}
})
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.