当我使用jquery在刀片中单击循环内的按钮时,我想获取数组的值索引,但是当我单击按钮并将其发送给控制器时,变量始终为空
刀片中的代码
<form class='form-horizontal' method='post' id="form" enctype="multipart/form-data" action={{CRUDBooster::mainpath('inputForm')}}>
{{ csrf_field() }}
<input type='hidden' name='return_url' value={{url('admin/list_kodefikasi_pengujian')}}/>
<input type='hidden' name='ref_mainpath' value={{url('admin/list_kodefikasi_pengujian')}}/>
.......
<tbody>
@foreach($detilKodefikasi AS $row)
<tr>
<td>{{ $row->jenis_pnpb }}</td>
<td>{{ (@$row->varietas!='')?$row->varietas:'-' }}</td>
<td>{{ (@$row->kode_sampel!='')?$row->kode_sampel:'-' }}</td>
<td>{{ (@$row->lot!='')?$row->lot:'-' }}</td>
<td>{{ $row->val_str }}</td>
<input type="hidden" name="id_jenisuji[]" value="{{$row->fid_jenisuji}}">
<input type="hidden" name="jenis_pnpb[]" value="{{$row->jenis_pnpb}}">
<input type="hidden" name="varietas[]" value="{{$row->varietas}}">
<input type="hidden" name="kode_sampel[]" value="{{$row->kode_sampel}}">
<input type="hidden" name="lot[]" value="{{$row->val_str}}">
<td><input type="submit" name="submit" value='Create' id="btn-save" class='btn btn-success btn-save'></td>
</tr>
@endforeach
</tbody>
........
</form>
我有JQuery
$("#form").on('submit',(function(e){
var indexIdJenisUji = $(this).closest("tr").find("input[name='id_jenisuji']").val();
e.preventDefault();
$.ajax({
url: "{{url('admin/list_kodefikasi_pengujian/inputForm')}}",
type: "POST",
data: form.serialize()+"&idJenisUji="+indexIdJenisUji },
success : function(data){
function() {
window.location = "{{url('admin/list_kodefikasi_pengujian')}}";
}
},
error: function(data){
alert(data.error->first());
}
});
}));
在控制器中
function inputForm()
{
$this->cbLoader();
$request = Request::all();
$idJenisUji = $request['idJenisUji'];
var_dump(idJenisUji);
}
总是返回null,但是当我创建新函数并向用户发出警报时,它将返回值
$(document).ready(function() {
$("#submit").click(function(){
var indexIdJenisUji = $(this).closest("tr").find("input[name='id_jenisuji']").val();
alert(indexIdJenisUji);
});
});
因此,如果我输入var indexIdJenisUji = $(this).closest(“ tr”)。find(“ input [name ='id_jenisuji']”)。val();在表单提交并与form.serialize()合并时,值始终返回null
答案 0 :(得分:1)
您的选择器需要使用括号[]
var indexIdJenisUji = $(this).closest("tr").find("input[name='id_jenisuji[]']").val();
或
var indexIdJenisUji = $(this).closest("tr").find("input[name^='id_jenisuji']").val();