我的 html 中有这段代码,我要在其中上传三个文件。除非选择文件,否则所有相应文件上传的提交按钮将被禁用。
$(document).ready(
function() {
$('input:file').change(
function() {
if ($(this).val()) {
$('input:submit').attr('disabled', false);
}
}
);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="#" method="post">
<input type="file" name="fileInput1" id="fileInput1" />
<input type="submit" value="submit" disabled />
</form>
<form action="#" method="post">
<input type="file" name="fileInput2" id="fileInput2" />
<input type="submit" value="submit" disabled />
</form>
<form action="#" method="post">
<input type="file" name="fileInput3" id="fileInput3" />
<input type="submit" value="submit" disabled />
</form>
我担心的是,如果我为第一个表单选择一个文件,其他表单中的提交按钮也会被启用。
最佳答案
选项 1: 无论元素的顺序如何,这都将启用所需的按钮:
$(document).ready(
function(){
$('form input:file').change(
function(){
if ($(this).val()) {
// Select button of this form
$(this).parent('form')
.children('input:submit')
.attr('disabled',false);
}
}
);
});
看这个jsFiddle
选项 2:如果 Input-submit 始终位于 Input-File 旁边,这将启用它:
$(this).next().attr('disabled',false);
而不是这个:
$('input:submit').attr('disabled',false);
看这个jsFiddle
关于javascript - 启用多个文件上传的按钮 - HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51012818/