首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >启用多个文件上载的按钮- HTML

启用多个文件上载的按钮- HTML
EN

Stack Overflow用户
提问于 2018-06-25 02:31:27
回答 1查看 67关注 0票数 1

我的html中有这段代码,我要上传三个文件。除非选择了文件,否则所有相应文件上载的提交按钮都会被禁用。

代码语言:javascript
复制
$(document).ready(
  function() {
    $('input:file').change(
      function() {
        if ($(this).val()) {
          $('input:submit').attr('disabled', false);
        }
      }
    );
  });
代码语言:javascript
复制
<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>

我担心的是,如果我为第一个表单选择一个文件,其他表单中的提交按钮也会被启用。

EN

回答 1

Stack Overflow用户

发布于 2018-06-25 02:41:31

您需要使用遍历,在正确的表单中找到正确的按钮。

代码语言:javascript
复制
$(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $(this).siblings('input[type=submit]').attr('disabled',false); 
                } 
            }
            );
    });

这将只查找作为文件输入兄弟的输入,这意味着它们在相同的父元素中(在您的示例中,是相同的表单)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51012818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档