如何在Laravel Blade中正确实现这个脚本?

huangapple go评论62阅读模式
英文:

How do I implement this script correctly in Laravel Blade?

问题

I am trying to make a registration form where different options become available depending on the radio button selected. However, when I try what I can find on the internet, I get an error. For the HTML part, I use:

<input type="radio" id="Swimmer" name="userType" value="swimmer" onclick="swimmerCheck()">
<label for="Swimmer">Swimmer</label><br>
<input type="radio" id="Coach" name="userType" value="coach">
<label for="Coach">Coach</label><br>
<input type="radio" id="Admin" name="userType" value="admin">
<label for="Admin">Admin</label><br>

<div id="fedNumber" style="display: none;">
  <label for="federation_number">{{ __('Fed Numb') }}</label>
  <input id="federation_number" class="block mt-1 w-full" type="text" name="federation_number" :value="old('federation_number')" autofocus autocomplete="name" />
</div>

And at the bottom, for the script, I use:

@push('script')
<script>
  console.log('hello');
  function swimmerCheck() {
    if (document.getElementById('Swimmer').checked) {
      console.log('Checked');
    } else {
      console.log('Not checked');
    }
  }
</script>
@endpush

But when I press the button and look at my console, it gives an error regarding the onclick, saying it can't read the properties.

I have made sure that the @stack('script') is also in the external style, and the 'Hello' console log does work, so the script does get through.

英文:

I am trying to make a registration form where different options become available depending on the radio button selected. However when i try what i can find on the internet i get an error. For the HTML part i use:

&lt;input type=&quot;radio&quot; id=&quot;Swimmer&quot; name=&quot;userType&quot; value=&quot;swimmer&quot; onclick=&quot;swimmerCheck()&quot;&gt;
&lt;label for=&quot;Swimmer&quot;&gt;Swimmer&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;radio&quot; id=&quot;Coach&quot; name=&quot;userType&quot; value=&quot;coach&quot;&gt;
&lt;label for=&quot;Coach&quot;&gt;Coach&lt;/label&gt;&lt;br&gt;
&lt;input type=&quot;radio&quot; id=&quot;Admin&quot; name=&quot;userType&quot; value=&quot;admin&quot;&gt;
&lt;label for=&quot;Admin&quot;&gt;Admin&lt;/label&gt;&lt;br&gt;

&lt;div id=&quot;fedNumber&quot; style=&quot;display: none;&quot;&gt;
&lt;x-label for=&quot;federation_number&quot; value=&quot;{{ __(&#39;Fed Numb&#39;) }}&quot; /&gt;
&lt;x-input id=&quot;federation_number&quot; class=&quot;block mt-1 w-full&quot; type=&quot;text&quot; name=&quot;federation_number&quot; :value=&quot;old(&#39;federation_number&#39;)&quot; autofocus autocomplete=&quot;name&quot; /&gt;
&lt;/div&gt;

And at the bottom, for the script i use:

@push(&#39;script&#39;)
&lt;script&gt;
console.log(&#39;hello&#39;);
function swimmerCheck() {
if (document.getElementById(&#39;Swimmer&#39;).@checked(true)) {
 console.log(&#39;Checked&#39;);
}
 else console.log(&#39;Not checked&#39;);

}
&lt;/script&gt;
@endpush

But when i press the button and look at my console, it gives an error regarding the onclick, saying it can't read the properties.

I have made sure that the @Stack('script') is also in the external style and the 'Hello' console log does work, so the script does really get through.

答案1

得分: 1

在您的父Blade文件上添加@stack(&#39;script&#39;)

英文:

Add @stack(&#39;script&#39;) on your parent blade file.

答案2

得分: 1

Laravel 显然不喜欢我的自定义 Blade 文件中的代码,但当我将它放在原始样式页面头部的脚本中时,它确实起作用。因此,不使用 @Stack 和 @Push。

英文:

So apperently Larvel didn't like my code in my custom blade file but it did work when i put it in a script in the head of the original styling page. So no use of @Stack and @Push.

huangapple
  • 本文由 发表于 2023年5月7日 04:42:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76191044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定