英文:
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:
<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;">
<x-label for="federation_number" value="{{ __('Fed Numb') }}" />
<x-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(true)) {
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 really get through.
答案1
得分: 1
在您的父Blade文件上添加@stack('script')
。
英文:
Add @stack('script')
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论