【发布时间】:2019-07-01 04:58:22
【问题描述】:
@extends('layouts.app')
@section('content')
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<div class="form-group col-md-8">
<button type="button" class="form-control" data-toggle="modal" data-target="#myModal">
Category </button>
<!-- The Modal -->
<div class="modal" id="myModal">
<div class="modal-dialog modal-lg" >
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<a type="button" class="close" data-dismiss="modal">×</a>
</div>
<div class="modal-body">
<div class="row">
<table class="table table-striped">
<thead>
</thead>
<tbody class="table">
<tr>
<td style="background-color: green">
<div class="col-md-4" >
{{-- <a href="#" name="category" id="category" > --}}
@foreach($categories as $category)
<option class="categoryList" value="{{$category->id}}">{{$category->category}}</option>
@endforeach
{{-- </a> --}}
</div>
</td>
<td>
<div class="col-md-4 ">
<a href="#" name="subcategory" id="subcategory" class="subcategoryList"> </a>
</div>
</td>
<td>
<div class="col-md-4">
<a href="#" name="subcategorytwo" id="subcategorytwo" ></a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(function(){
$('.categoryList').on('click', function(){
//alert('hi');
var cat_id = $(this).attr('value');
console.log(cat_id);
var url = "/api/getSubcategory/"+cat_id;
$.ajax({
type: "GET",
url: url,
dataType: "JSON",
success: function(res)
{
// amusing res = {"3":"home","4":"home duplex"};
var html = "";
$.each(res, function (key, value) {
html += "<option value="+key+">"+value+"</option>";
});
$("#subcategory").html(html);
}
});
});
});
$(function(){
$('.subcategoryList').on('click', function(){
//alert('hi');
var subcat_id = $(this).attr('value');
console.log(cat_id);
var url = "/api/getSubcategorytwo/"+subcat_id;
$.ajax({
type: "GET",
url: url,
dataType: "JSON",
success: function(res)
{
var xxyz = "";
$.each(res, function (key, value) {
xyz += "<option value="+key+">"+value+"</option>";
});
$("#subcategorytwo").xyz(xyz);
}
});
});
});
</script>
@endsection
这是我的 API
public function getSubcategory($id){
$subcategories = Subcategory::where('category_id',$id)->select('subcategory','id')->pluck('subcategory', 'id');
return json_encode($subcategories);
}
public function getSubcategorytwo($id){
$subcategorytwos = Subcategorytwo::where('subcategory_id',$id)->select('subcategorytwo','id')->pluck('subcategorytwo', 'id');
return json_encode($subcategorytwos);
}
这是 API 路由
Route::get('api/getSubcategory/{id}', 'PostController@getSubcategory' );
Route::get('api/getSubcategorytwo/{id}', 'PostController@getSubcategorytwo' );
问题是当我单击类别打开子类别但当我单击子类别时未打开与子类别相关的他 This is category table
ajax有什么问题,即使我改变变量html替换xyz.. 我正在提供完整的细节。因为我的英语很差。 表、视图、控制器、路由都已定义,当我单击类别打开子类别时,但当我单击子类别时,它并没有打开与子类别相关的。
【问题讨论】:
-
您在浏览器控制台中看到了什么?
-
当我单击子类别时,控制台只看到未定义 -@ManzurulHoqueRumi
-
检查我的完整答案并遵循它。
-
我应用您的答案...即使子类别也不会显示,并且控制台错误像这样创建:1 [违规] 将非被动事件侦听器添加到滚动阻止“鼠标滚轮”事件。考虑将事件处理程序标记为“被动”以使页面更具响应性。见chromestatus.com/feature/5745543795965952 -@ManzurulHoqueRumi
标签: javascript laravel laravel-5 laravel-4 laravel-5.2