我正在尝试根据laravel 5的另一个名为field1的下拉列表的值创建名为field2的动态下拉列表。问题是当我更改字段1时,field2没有任何反应。有什么问题?
这是我的表格:
<form action="/automations" method="POST" class="form-horizontal" id="automationForm">
{{ csrf_field() }}
<!-- Task Name -->
<div class="form-group">
<label for="task-name" class="col-sm-3 control-label">Field 1</label>
<div class="col-sm-6">
{!!
Form::select(
'field1',
(['0' => 'Select a Field'] + $fields),
(isset($field->id) ? $field->id : null),
['class' => 'form-control']
)
!!}
</div>
</div>
<!-- Task Name -->
<div class="form-group">
<label for="task-name" class="col-sm-3 control-label">Field 2</label>
<div class="col-sm-6">
{!!
Form::select(
'field2',
(['0' => 'Select a Field'] + $fields),
(isset($field->id) ? $field->id : null),
['class' => 'form-control']
)
!!}
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<button type="submit" class="btn btn-default">
<i class="fa fa-plus"></i> Add Automation
</button>
</div>
</div>
</form>
</div>
这是我的js代码:
<script>
jQuery(document).ready(function($){
$('#field1').change(function(){
$.get("{{ url('api/dropdown')}}",
{ option: $(this).val() },
function(data) {
$('#field2').empty();
$.each(data, function(key, element) {
$('#field2').append("<option value='" + key +"'>" + element + "</option>");
});
});
});
});
</script>
这是routes.php文件:
Route::get('api/dropdown', function(){
$id = Input::get('option');
if($id == 0) {
$field2 = array(
1 => 'Enable',
2 => 'Pause',
);
} else {
$field2 = array(
1 => 'Not Enable',
2 => 'Pause',
);
}
return $field2;
});