选择下拉列表后如何进行自动填充。我不太擅长js或ajax。
当用户选择doc_no时,必须同时填写rev_no和title字段。谢谢!
视图
<div class="form-group">
{!! Form::label('text', 'Doc No', ['class' => 'col-lg-3 control-label']) !!}
<div class="col-lg-10">
<select name="docNo" id="docNo" class="form-control" style="width:250px">
@foreach ($soplists as $soplist)
<option value="{{ $soplist->id }}">{{ $soplist->doc_no }}</option>
@endforeach
</select>
</div>
</div>
<input type="hidden" name="carType" value="Internal Audit" class="form-control">
<div class="form-group">
{!! Form::label('text', "Rev No", ['class' => 'col-lg-5 control-label']) !!}
<div class="col-lg-5">
<input type="text" class="form-control" id="rev" />
</div>
</div>
<div class="form-group">
{!! Form::label('text', "Title", ['class' => 'col-lg-5 control-label']) !!}
<div class="col-lg-10">
<input type="text" class="form-control" id="title" />
</div>
<script>
$('#docNo').change(function() {
var id = $(this).val();
var url = '{{ route("getDetails", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response) {
if (response != null) {
$('#rev').val(response.rev_no);
$('#title').val(response.title);
}
}
});
});
</script>
控制者
--- PHP
public function getDetails($id = 0)
{
$data = sopList::where('doc_no', $id)->first();
echo json_encode($data);
exit;
}
路线
'Route::get('get/details/{id}', 'internalAuditController@getDetails')->name('getDetails');'
数据库sop_list表图像链接
https://ibb.co/SwkJhLc
下拉菜单和输入图像
https://ibb.co/0VN3Z2y
网络标签
https://ibb.co/56w5BLD
最佳答案
在web.php
文件中添加路由:
Route::get('get/details/{id}', 'YourController@getDetails')->name('getDetails');
控制器功能:
public function getDetails($id=0){
$data = sopList::where('doc_no',$id)->first();
echo json_encode($data);
exit;
}
并查看脚本:
$('#docNo').change(function(){
var id = $(this).val();
var url = '{{ route("getDetails", ":id") }}';
url = url.replace(':id', id);
$.ajax({
url: url,
type: 'get',
dataType: 'json',
success: function(response){
if(response != null){
$('#rev').val(response.rev_no);
$('#title').val(response.title);
}
}
});
});
确保将ID
rev
和title
分别添加到rev
和title
输入字段中。关于mysql - Laravel-从下拉列表中选择值后自动填充输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57987601/