我试图通过从 <input type="file" />
中选择的文件名来更改 <label>
标签中的文本。
我尝试了下面的方法,但是没有成功:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery-1.12.4.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$('#files').on("change",function() {
console.log("change fired");
var i = $(this).prev('label').clone();
var file = $('#files')[0].files[0].name;
console.log(file);
$(this).prev('label').text(file);
});
</script>
</head>
<body>
<div class="upload_firmware_container">
Please upload the provided <span style="color:#e11422">firmware.bin</span> file and/or <span style="color:#e11422">spiffs.bin</span> file.</br>
<!-- action="/doUpdate" -->
<form method="POST" enctype="multipart/form-data">
<label class="file_input_label" for="files">Browse files</label>
<input id="files" type="file" name="update">
<input class="upload_button" type="submit" name="getUpdate" value="Update">
</form>
</div>
</body>
</html>
如您所见,我有两个脚本来源。第一个是本地的,它可以与我所有的小脚本一起使用。我添加了第二个,因为我在灵感来源的示例中发现了它。
您认为呢? 欢迎使用jQuery。
解决方法:
它没有起作用,因为脚本必须在相关元素之后。
将脚本移动到 HTML
页面的末尾解决了一切问题。
window.onload
事件,并将你的代码写在该事件中。 - Krishna Prashatt.js
文件,然后在关闭</body>
标记之前调用该文件。还要记住,如果您有多个.js
文件,在HTML文件中调用它们时顺序很重要。 - Sergio