[英]Setting the selected value of a dropdown option
I'm working on a simple calculator app in Laravel Blade.我正在 Laravel Blade 中开发一个简单的计算器应用程序。 We have not moved to models yet, just working in views and routes, so I keep running into options I haven't learned how to use yet.
我们还没有转向模型,只是在视图和路由中工作,所以我不断遇到我还没有学会如何使用的选项。
My application is running without issue but is not retaining the selected value in the dropdown on POST.我的应用程序运行没有问题,但未在 POST 的下拉列表中保留所选值。 I am able to print the value to the screen, and it is working in a later selector.
我能够将值打印到屏幕上,并且它在以后的选择器中工作。 I think I just need to write an if statement in the options to set the selected value, but I can't find syntax that I understand/am allowed to use in this project.
我想我只需要在选项中编写一个 if 语句来设置所选值,但我找不到我理解/允许在这个项目中使用的语法。
<div class="form-group">
<select class="form-control form-control-lg" id="operatorInput" name="operatorInput" value="{{Session::get('operator')}}">
<option value="+" @if(Session::get('operatorInput') == "+" ? "selected" : "" )@endif>Addition (+)</option>
<option value="-" @if(Session::get('operatorInput') == "-" ? "selected" : "" )@endif>Subtraction (-)</option>
</select>
</div>
I get a throwable error on this example, so I know it isn't correct.我在这个例子中得到了一个可抛出的错误,所以我知道它是不正确的。
The throwable error is due to calling the Session Facade without the \ root indicator, use the session helper instead throwable 错误是由于在没有 \ 根指示符的情况下调用 Session 外观,请改用 session 助手
For example routes/web.php
例如
routes/web.php
Route::get('/', function () {
session(['operatorInput' => '-']);
return view('welcome');
});
And welcome.blade.php
和
welcome.blade.php
<div class="form-group">
<select class="form-control form-control-lg" id="operatorInput" name="operatorInput"
value="{{ session('operator') }}">
<option value="+" {{ (session('operatorInput') == "+") ? "selected" : "" }}>Addition (+)</option>
<option value="-" {{ (session('operatorInput') == "-") ? "selected" : "" }}>Subtraction (-)</option>
</select>
</div>
You can keep your current code but you need to add a backslash \
before the \Session
facade alias, but I find the helper function to be more elegant for a view file (and shorter)您可以保留当前代码,但需要在
\Session
外观别名之前添加反斜杠\
,但我发现帮助程序 function 对于视图文件更优雅(并且更短)
Hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.