Php Laravel动态下拉列表不为空
我的目标是实现一个动态下拉列表,它将在第一个下拉列表的基础上显示第二个下拉列表的值 一个服务有很多类别,一个类别属于一个服务,所以第二个下拉列表是类别,第一个下拉列表是服务。选择服务时,它将显示基于所选服务的所有类别 下面是RequestController.php中的代码 在my fields.blade.php中 在ServiceController.php中 我用Pull获得了所有的数据。但它是一个空表单,用于名为category\u id的类别 所以我想我的问题在剧本里还是不 如果有人能帮忙,我将不胜感激。Php Laravel动态下拉列表不为空,php,laravel,laravel-5,Php,Laravel,Laravel 5,我的目标是实现一个动态下拉列表,它将在第一个下拉列表的基础上显示第二个下拉列表的值 一个服务有很多类别,一个类别属于一个服务,所以第二个下拉列表是类别,第一个下拉列表是服务。选择服务时,它将显示基于所选服务的所有类别 下面是RequestController.php中的代码 在my fields.blade.php中 在ServiceController.php中 我用Pull获得了所有的数据。但它是一个空表单,用于名为category\u id的类别 所以我想我的问题在剧本里还是不 如果有人能
提前感谢。,我不完全确定问题出在哪里,但听起来好像是正确地从select中清除了数据,但没有将其放回去 可能是追加到已更改的元素时出现问题,或者找不到正确的选择器,或者它不是动态的,在“更改”与“更改”之间进行更改后会丢失其挂钩。。。或者一些事情 这是我用来重新填充的函数。我在循环中添加html,然后使用hook将整个select一次性更改为$this,然后触发更改 也许这可以帮助您:
// After filtering (any select box across the program), repopulate it -- same function for anything
function ajaxFilterSuccess(typeClass, newArray){
$('.'+typeClass).each(function(i, obj) {
// Only add Please Select to single selects (not multiple / select2) since multis are usually
// many to many pivots without a way to mutate the input away from '' (needs to be null or stopped totally)
if(typeof this.attributes['multiple'] == 'undefined')
var list = '<option value="">Please Select</option>';
if ($(this).val() != null) {
$(this).children(':selected').each(function() {
// Use native JS to get 'this', so it doesn't confuse with the $(this) selector looping
list += '<option value="'+this.value+'" selected>'+this.text+'</option>';
});
}
$(this).empty();
$.each(newArray, function(value,key) {
list += "<option value='" +value + "'>" + key + "</option>";
});
$(this).html(list);
$(this).trigger('change');
});
}
试试这个,我有一些相似的东西,也有一些小的不同
<script>
JQuery(function() {
$('#service_id').change(function() {
var url = '{{ url('service') }}' + $(this).val() + '/categories/';
$.get(url, function(data) {
var select = $('form select[name= category_id]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + key + '>' + value + '</option>');
});
});
});
});
</script>
它已经起作用了。但它在下拉列表中返回undefined。但这一目标正在实现。关系的编号正在显示,但在所有名称及其值上都是未定义的。它返回未定义的值。当我通过静态转到该路由对其进行测试时,它返回正确的值。但是在select下拉列表中返回undefined。我在每个循环中都做了修改。我昨天忽略了它。对不起。现在应该可以了。
<script>
$(function() {
$('select[name=service_id]').change(function() {
var url = '{{ url('service') }}' + $(this).val() + '/categories/';
$.get(url, function(data) {
var select = $('form select[name= category_id]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + value.id + '>' + value.name + '</option>');
});
});
});
});
</script>
Route::get('service/{service}/categories', 'ServiceController@getCategories');
public function getCategories($service)
{
$service = Service::findOrFail($service);
return $service->categories->pluck(['id','name']);
}
// After filtering (any select box across the program), repopulate it -- same function for anything
function ajaxFilterSuccess(typeClass, newArray){
$('.'+typeClass).each(function(i, obj) {
// Only add Please Select to single selects (not multiple / select2) since multis are usually
// many to many pivots without a way to mutate the input away from '' (needs to be null or stopped totally)
if(typeof this.attributes['multiple'] == 'undefined')
var list = '<option value="">Please Select</option>';
if ($(this).val() != null) {
$(this).children(':selected').each(function() {
// Use native JS to get 'this', so it doesn't confuse with the $(this) selector looping
list += '<option value="'+this.value+'" selected>'+this.text+'</option>';
});
}
$(this).empty();
$.each(newArray, function(value,key) {
list += "<option value='" +value + "'>" + key + "</option>";
});
$(this).html(list);
$(this).trigger('change');
});
}
<script>
JQuery(function() {
$('#service_id').change(function() {
var url = '{{ url('service') }}' + $(this).val() + '/categories/';
$.get(url, function(data) {
var select = $('form select[name= category_id]');
select.empty();
$.each(data,function(key, value) {
select.append('<option value=' + key + '>' + value + '</option>');
});
});
});
});
</script>
public function getCategories($service)
{
$service = Service::findOrFail($service);
return $service->categories->pluck('name', 'id');
}