jquery - 未选择文件时如何使按钮变灰

标签 jquery css

我已经设法在未选择要上传的文件时禁用该按钮,但我想更改它在禁用/启用时的外观(btn-secondary,btn-primary)

我尝试更改 .css 文件,但它对我不起作用

我只希望当没有选择文件时按钮呈灰色:https://getbootstrap.com/docs/4.0/components/buttons/ btn中学

$(document).ready(function() {
  $('input:file').change(function() {
    if ($(this).val()) {
      $('input:submit').attr('disabled', false);
    }
  });
});
.disabled {
  color: #999;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<td width="30%">
  <input type="file" name="file" id="profile-img" />
</td>
<td width="30%" align="left">
  <input type="submit" name="upload" class="btn btn-primary" value="Upload" disabled>
</td>

最佳答案

使用[attr] in css通过属性选择 DOM

$(document).ready(
    function(){
        $('input:file').change(
            function(){
                if ($(this).val()) {
                    $('input:submit').attr('disabled',false);
                }
            }
        );
    });
input[disabled] {
    color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="file" name="file" id="profile-img"/>
<input type="submit" name="upload" class="btn btn-primary" value="Upload" disabled>

编辑您的评论

I tried adding background-color: darkgray; but it didn't work

因为您使用 Bootstrap ,所以您需要使用 !important 覆盖 backround

$(document).ready(
  function() {
    $('input:file').change(
      function() {
        if ($(this).val()) {
          $('input:submit').attr('disabled', false);
        }
      }
    );
  });
input[disabled] {
  color: red;
  background: darkgray!important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<input type="file" name="file" id="profile-img" />
<input type="submit" name="upload" class="btn btn-primary" value="Upload" disabled>

使用 !important is not really good . 所以你可以改变the order of your style sheets links

或设置 specific idinput 并在 css 中使用它:

$(document).ready(function() {
  $('input:file').change(function() {
    if ($(this).val()) {
      $('input:submit').attr('disabled', false);
    }
  });
});
#btn-uplaod[disabled] {
  color: red;
  background: darkgray;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
<input type="file" name="file" id="profile-img" />
<input type="submit" name="upload" class="btn btn-primary" id="btn-uplaod" value="Upload" disabled>

关于jquery - 未选择文件时如何使按钮变灰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56273864/

相关文章:

javascript - 如何在此函数中有效地使用 "this"以便它不会影响其他点击事件?

javascript - 使用 jquery 将样式设置为 div 中的段落

jquery - 根据返回的记录将字段呈现为 jQuery 自动完成或下拉列表

html - 在不破坏布局的情况下将 div 对齐到底部

html - 对齐标注旁边的按钮

jquery - Laravel 路由中的 Ajax 调用返回包含 ChartJS 标签日期的 json 编码数组

javascript - 如何跳过卸载前弹出的确认窗口并显示我自己的 Bootstrap 模式?

javascript - 菜单栏无法正常工作

html - 表行将不包含位置为 :absolute 的元素

javascript - 滑动菜单工作正常但有双重动画