Append Selected Drop Down Value with Option in Laravel Blade with Jquery

只谈情不闲聊 提交于 2019-12-13 03:23:43

问题


I want to show the models by changing the Drop Down value....I know how to get the value of drop down with change function but my problem is that i want to send that value in route to filter the models.....Please tell me how can i send the changed value id with route from blade..

This is my Drop Down

<select class="form-control" id="category" placeholder="Select category">
       <option>All</option>
       @foreach($categories as $category)
      <option value="{{route('contributors.index')}}?category={{ $category->id }}">{{$category->name}}</option>
      @endforeach
</select>

Here i my Jquery Change Function

  $('#category').change(function(){
            alert($(this).val());
        })

Explanation :

Please check the below picture....I want to show the models(in the same view)which are related to that category selected in the drop down.


回答1:


Add this to redirect you to the specific route. For you, it refreshes the page with the selected id in URL. So, models will filter for the given category_id.

$('#category').change(function(){
    window.location = "domain.com/route/" + $('option:selected', this).val();
})

Of course, you should use your own URL instead of the URL in the example.




回答2:


You can use ajax call to append and send category value to your route

$( "#category" ).change(function() {
  var id=$(this).val();

    $.ajax({
        type: 'GET',
        url: your url goes here,
        success: function (data) {
           //here yuo can append to model div
        },
        error: function (data) {

            console.log(data);

        }
    });

});

the above code is sample for ajax request



来源:https://stackoverflow.com/questions/46501672/append-selected-drop-down-value-with-option-in-laravel-blade-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!