【问题标题】:Why does the absence of an error in Laravel Blade $errors array break my JavaScript为什么 Laravel Blade $errors 数组中没有错误会破坏我的 JavaScript
【发布时间】:2017-06-30 01:47:58
【问题描述】:

我有一个LaravelBlade 文件,我想了解nameemailmessage 字段中的错误。 The has method may be used to determine if any error messages exist for a given field。我有一个JavaScript 函数indicateErrorBox,它向表单添加了一些CSS 类。

如果 ALL 字段有错误,但如果有一个或多个没有,则代码不会运行。它似乎什么也没做。

<script>
window.onload = function() {
  if ({{$errors->has('name')}}) {
    indicateErrorBox("name");
  }
  if ({{$errors->has('email')}}) {
    indicateErrorBox("email");
  }
  if ({{$errors->has('message')}}) {
    indicateErrorBox("message");
  }
};
</script>

我显然做错了什么。谁能发现我的错误?

【问题讨论】:

  • 通过查看生成的源浏览器看到并在浏览器控制台中查看抛出的错误,应该很容易调试自己。生成的源代码是什么样的?

标签: javascript php forms laravel validation


【解决方案1】:

试试这个:

<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>

【讨论】:

  • 检查您生成的源代码 html/js 以查看 {{$errors->has(...)}} 输出。如果它像您的 js 代码期望的那样输出 true 或 false,我会感到惊讶。这就是为什么我只是使用刀片 if/else 指令在条件通过时直接调用 JS 函数
  • 感谢您的解释
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-12
  • 2017-02-13
  • 1970-01-01
  • 2013-06-25
  • 2013-06-07
相关资源
最近更新 更多