Old value in multiple select option in laravel bla

2020-07-03 08:33发布

Here is my select option

<select name="recomemded_food[]" value="" style="width:560px;" multiple class="chosen-select" >
<option value="American Black Bear">American Black Bear</option>
<option value="Asiatic Black Bear">Asiatic Black Bear</option>
<option value="Brown Bear">Brown Bear</option>
<option value="Giant Panda">Giant Panda</option>
</select>

And below is my code trying to use the foreach loop to get the array value. but I am receiving the following error:

@foreach (explode(',',old('recomemded_food')) as $recomemded_food) 
{{$recomemded_food}}
@endforeach

Error Message : explode() expects parameter 2 to be string

7条回答
2楼-- · 2020-07-03 09:04

This is the way I do, very dynamic.

<select id="gender" name="gender">
    <option>Select</option>
        <option value="M">Male</option>
        <option value="F">Female</option>
</select>

<script>
    var currentGender = null;
    for(var i=0; i!=document.querySelector("#gender").querySelectorAll("option").length; i++)
    {
        currentGender = document.querySelector("#gender").querySelectorAll("option")[i];
        if(currentGender.getAttribute("value") == "{{ old("gender") }}")
        {
            currentGender.setAttribute("selected","selected");
        }
    }
</script>
查看更多
登录 后发表回答