javascript - 启用多个文件上传的按钮 - HTML

标签 javascript html

我的 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/

相关文章:

javascript - knockout ,太多无容器 if 语句

javascript - 替换html中的所有单词而不影响标签

html - 使用 '#'(标签)发送一条消息

javascript - 如何使用 javascript 在 DIV 框内写入

javascript - Uncaught Error : [$injector:unpr] Unknown provider: $animateProvider

javascript - Jquery 如果窗口宽度 < something and p string > something

html - 强制执行可选包含图像的 td 的(最小)宽度

javascript - 从表 td 中获取所有属性作为数组

javascript - push 和 length 数组操作,遍历数组

javascript - JQuery - 查找具有给定数量兄弟元素的元素