[英]Display the input when a specific option is selected in laravel
I am using Laravel 5.2 . 我正在使用Laravel 5.2 。 I have an input to display when a specific option is selected and it works fine.
选择特定选项后,我可以显示输入内容,并且效果很好。
But I want to use {{ old('field') }} to have the old results. 但是我想使用{{old('field')}}获得旧结果。
When I have validation errors (server side) and I return to the page, then I have the old results (example: value='yes') but the input that was displaying is now hidden and I have to re-click on yes to display it again. 当我遇到验证错误(服务器端)并返回页面时,我得到的是旧结果(例如:value ='yes'),但是现在显示的输入已隐藏,我必须重新单击yes再次显示。 I use the change event to display the input.
我使用change事件显示输入。
$("#etat_civil").change(function () {
if ($(this).val() == "Marié") {
$("#div_conjoint").show();
} else {
$("#div_conjoint").hide();
}
});
You can check the old('field') value for it is set then you can set your element to be visible. 您可以检查old('field')值是否已设置,然后可以将元素设置为可见。
For example, you can do something like this. 例如,您可以执行以下操作。
<input type="text" name="my-field" value="abc" style="display: {{ old('field') ? 'block' : 'none' }}">
By doing this you are checking if old('field') is set then the field/block/div should be visible otherwise keep it hidden. 通过这样做,您正在检查是否设置了old('field') ,那么field / block / div应该是可见的,否则将其隐藏。
So in your case when the validation fails and it redirects you back to the form you will have old('field') value set and the element should be visible to you. 因此,在您的情况下,当验证失败并且它将您重定向回表单时,您将设置old('field')值,并且该元素对您应该可见。
Update: 更新:
As suggested by other guys you can also do this by putting the JS code on page onload but I don't prefer that way as it sometimes shows a slight delay in loading the element. 正如其他人所建议的那样,您也可以通过在页面onload上放置JS代码来执行此操作,但我不喜欢这种方式,因为有时加载元素时会稍有延迟。 Not a great deal but I still prefer doing it the other way that I explained above.
没什么大不了的,但是我还是喜欢用上面解释过的其他方式来做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.