如何在 Laravel 中重定向后设置选择值

我是 Laravel 的新手,我试图将 select 的值设置为从我的控制器返回的值。我不明白为什么它不会改变价值


显示.blade.php


<div class="container d-flex justify-content-center">

    <form action="{{ action('ReportController@show', ['id' => 'month']) }}" class="month-form" method="GET">

        <select name="month" id="month" class="custom-select form-input month-select">

            <option value="01">January</option>

            <option value="02">February</option>

            <option value="03">March</option>

            <option value="04">April</option>

            <option value="05">May</option>

            <option value="06">June</option>

            <option value="07">July</option>

            <option value="08">August</option>

            <option value="09">September</option>

            <option value="10">October</option>

            <option value="11">November</option>

            <option value="12">December</option>

        </select>

        <button class="btn btn-secondary">Generate Report</button>

    </form>

</div>

控制器 功能展示()


$month_number = request('month');

$month_name = date("M", mktime(0, 0, 0, $month_number, 1));

return view('reports.show', compact('month_number', 'month_name'));

这是我的表单 当我单击生成按钮时,它会返回到此页面并且值将是我之前选择的那个。因此,如果我选择 May,然后选择生成按钮,新页面将是 May,而不是默认值。


编辑:我也试过使用 jQuery,但它只会让我的表单空白,根本不会给它任何价值


$(document).ready(function(){

   var month = @json($month_name ?? '');

   $('#month').val(month);

});


不负相思意
浏览 106回答 1
1回答

炎炎设计

如果我清楚地理解你的问题,那么我想你想要这样的东西<div class="container d-flex justify-content-center"><form action="{{ action('ReportController@show', ['id' => 'month']) }}" class="month-form" method="GET">&nbsp; &nbsp; <select name="month" id="month" class="custom-select form-input month-select">&nbsp; &nbsp; &nbsp; &nbsp; <option value="01" @isset($month_number) {{ $month_number == 01 ? 'selected' : '' }} @endisset>January</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="02" @isset($month_number) {{ $month_number == 02 ? 'selected' : '' }} @endisset>February</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="03" @isset($month_number) {{ $month_number == 03 ? 'selected' : '' }} @endisset>March</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="04" @isset($month_number) {{ $month_number == 04 ? 'selected' : '' }} @endisset>April</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="05" @isset($month_number) {{ $month_number == 05 ? 'selected' : '' }} @endisset>May</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="06" @isset($month_number) {{ $month_number == 06 ? 'selected' : '' }} @endisset>June</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="07" @isset($month_number) {{ $month_number == 07 ? 'selected' : '' }} @endisset>July</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="08" @isset($month_number) {{ $month_number == 08 ? 'selected' : '' }} @endisset>August</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="09" @isset($month_number) {{ $month_number == 09 ? 'selected' : '' }} @endisset>September</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="10" @isset($month_number) {{ $month_number == 10 ? 'selected' : '' }} @endisset>October</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="11" @isset($month_number) {{ $month_number == 11 ? 'selected' : '' }} @endisset>November</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="12" @isset($month_number) {{ $month_number == 12 ? 'selected' : '' }} @endisset>December</option>&nbsp; &nbsp; </select>&nbsp; &nbsp; <button class="btn btn-secondary">Generate Report</button></form>如果它不起作用然后尝试更改01为"01"等等...
打开App,查看更多内容
随时随地看视频慕课网APP