[英]Laravel - javascript doesn't work inside @section blade (file input)
I'm working on a Laravel project.我正在研究 Laravel 项目。 I have an input for loading files and I'm basically trying to show the filename when the file is loaded (before sending the form).我有一个加载文件的输入,我基本上是在加载文件时(在发送表单之前)显示文件名。
I'm using this example https://codepen.io/hidde/pen/LyLmrG which works well on its own, but not inside my laravel views.我正在使用这个示例https://codepen.io/hidde/pen/LyLmrG ,它本身运行良好,但不在我的 laravel 视图中。
These are my files:这些是我的文件:
appHome.blade.php (basic layout) appHome.blade.php (基本布局)
<html>
...
<body class="font-body" >
...
@yield('content')
...
<script>
var input = document.getElementById( 'file-upload' );
var infoArea = document.getElementById( 'file-upload-filename' );
input.addEventListener( 'change', showFileName );
function showFileName( event ) {
var input = event.srcElement;
var fileName = input.files[0].name;
infoArea.textContent = 'File name: ' + fileName;
}
</script>
</body>
</html>
import-form.blade.php进口-form.blade.php
@extends('layouts.appHome')
@section('content')
...
...
<form>
<input type="file" id="file-upload" multiple required />
<label for="file-upload">Upload file</label>
<div id="file-upload-filename"></div>
</form>
@endsection
I tried to console.log inside the js script or check if the filename variable exists and it all works, but I think it doesn't work when it adds the filename to the infoArea (infoArea.textContent).我尝试在 js 脚本中进行 console.log 或检查文件名变量是否存在并且一切正常,但我认为将文件名添加到 infoArea (infoArea.textContent) 时它不起作用。
Is this related to blade section/extends or js?这与刀片部分/扩展或 js 有关吗?
Thanks!谢谢!
Try waiting for document ready state before you execute and javascript:在执行之前尝试等待文档准备好 state 和 javascript:
function ready(callbackFunc) {
if (document.readyState !== 'loading') {
// Document is already ready, call the callback directly
callbackFunc();
} else if (document.addEventListener) {
// All modern browsers to register DOMContentLoaded
document.addEventListener('DOMContentLoaded', callbackFunc);
} else {
// Old IE browsers
document.attachEvent('onreadystatechange', function() {
if (document.readyState === 'complete') {
callbackFunc();
}
});
}
}
ready(function() {
var input = document.getElementById( 'file-upload' );
var infoArea = document.getElementById( 'file-upload-filename' );
input.addEventListener( 'change', showFileName );
});
function showFileName( event ) {
var input = event.srcElement;
var fileName = input.files[0].name;
infoArea.textContent = 'File name: ' + fileName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.