我的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>
我担心的是,如果我为第一个表单选择一个文件,其他表单中的提交按钮也会被启用。
发布于 2018-06-25 02:41:31
您需要使用遍历,在正确的表单中找到正确的按钮。
$(document).ready(
function(){
$('input:file').change(
function(){
if ($(this).val()) {
$(this).siblings('input[type=submit]').attr('disabled',false);
}
}
);
});
这将只查找作为文件输入兄弟的输入,这意味着它们在相同的父元素中(在您的示例中,是相同的表单)。
https://stackoverflow.com/questions/51012818
复制相似问题