How to retrieve looped value attributes from

2020-05-01 06:58发布

问题:

I'm using the laravel framework and I created a drop down box and populated it with items from the database by looping.

<select class="form-control" id="username" name="username" onchange="handleSelect(elm)">     
                <option value="" selected>Select User</option>
                @foreach($getUsers as $list)
                <option value="{{$list->id}}" >{{$list->name}}</option>
                @endforeach

            </select>

I want when I click an item in the drop down list, the value of the item clicked is passed in a url. Basically I want to reload the page with the item clicked to make changes to the page. A way I thought of doing it was to pass the value of the option clicked to the handleSelect() function but I don't know how to do that.

edit: the handleSelect() doesn't do anything. I'm just trying to pass what is in the selected looped value attribute to the handleSelect() function.

回答1:

A very basic javascript function to handle the onchange event that is fired whenever the user selects an option from the menu

function handleSelect(e){
    /* find element name and assign the value */
    location.search=[ e.target.name, e.target.value ].join('=');
}