我有一个 Laravel
Blade
文件,我想了解 name
、email
或消息
字段。 The has method may be used to determine if any error messages exist for a given field 。我有一个 JavaScript
函数 indicateErrorBox
,它向表单添加了一些 CSS
类。
如果所有字段都有错误,它会完美运行,但如果一个或多个字段没有错误,则代码不会运行。它似乎什么也没做。
<script>
window.onload = function() {
if ({{$errors->has('name')}}) {
indicateErrorBox("name");
}
if ({{$errors->has('email')}}) {
indicateErrorBox("email");
}
if ({{$errors->has('message')}}) {
indicateErrorBox("message");
}
};
</script>
我显然做错了什么。有人能发现我的错误吗?
最佳答案
试试这个:
<script>
window.onload = function() {
@if ($errors->has('name'))
indicateErrorBox("name");
@endif
@if ($errors->has('email'))
indicateErrorBox("email");
@endif
@if ($errors->has('message'))
indicateErrorBox("message");
@endif
};
</script>
关于javascript - 为什么 Laravel Blade $errors 数组中没有错误会破坏我的 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180955/