在我的 Laravel-5.8 中,我在控制器中有这个 JSON get 请求代码:
public function findIdentity(Request $request)
{
$userCompany = Auth::user()->company_id;
$identity = DB::table('appraisal_identity')->select('id')->where('company_id', $userCompany)->where('id',$request->id)->first();
return response()->json([
'identity' => $identity->id
]);
}
查看刀片:
< script type = "text/javascript" >
$(document).ready(function() {
$(document).on('change', '#identity', function() {
var air_id = $(this).val();
var a = $(this).parent();
var op = "";
$.ajax({
type: 'get',
url: '{{ route('
get.identities.all ') }}',
data: {
'id': air_id
},
dataType: 'json', //return data will be json
success: function(data) {
console.log(data.identity);
$('#identity_id').val(data.identity);
},
error: function() {
}
});
});
}); <
/script>
<div class="row" style="margin-bottom: 10px">
<label class="control-label"> Performance Period:<span style="color:red;">*</span></label>
<div class="col-sm-8">
<select id="identity" name="identity_id" class="form-control">
<option value="">--- Select Performance Period ---</option>
@foreach ($identities as $ids => $name)
<option value="{{ $ids }}" {{ request( 'identity_id')==$ids ? 'selected' : ''}}>{{ $name }}</option>
@endforeach
</select>
</div>
<input type="hidden" id="identity_id" class="form-control">
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
<form action="{{ route('performance_dashboard', $id) }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<button type="submit" class="btn btn-secondary btn-sm" name="viewIdentity"><i class="fas fa-file-pdf"></i> View Identity</button>
</form>
</div>
</div>
路线:
Route::get('get/findIdentity','HomeController@findIdentity')->name('get.identities.all');
从上面的所有代码中,我试图将视图刀片中的下拉列表(选择选项)的 id 放入路由参数中。
如何将#identity_id 作为参数传递给 #id:
{{ route('performance_dashboard', $id) }}
答案 0 :(得分:0)
无法在 php 变量中设置 javascript 响应,但是下面提到了您可以做的一件事:
<div class="col-xs-4">
<button type="submit" class="btn btn-primary btn-sm" name="search">Search</button>
<form action="{{ route('performance_dashboard') }}" method="get" target="_blank" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<input type="hidden" id="identity_id" class="form-control"/>
<button type="submit" class="btn btn-secondary btn-sm" name="viewIdentity"><i class="fas fa-file-pdf"></i> View Identity</button>
</form>
</div>
</div>
现在在控制器中,您可以简单地获得 indentity_id。