如何检查是否在< input type =“文件">中选择了文件?元素? [英] How do I check if a file has been selected in a <input type="file"> element?

查看:76
本文介绍了如何检查是否在< input type =“文件">中选择了文件?元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个复选框和一个文件上传输入.如果要检查一个或多个复选框,并且输入值不为null,我想重新启用按钮.

I have multiple checkboxes and a file upload input. I would like to re-enable a button if one or more checkbox's are checked AND if the input value is not null.

这里是 bootply

这是我的html

<div class="upload-block">
    <input type="checkbox">
    <input type="checkbox">
    <input type="checkbox">
    <input type="file" id="InputFile">
    <button id="upload-btn" type="button blue-button" 
            class="btn blue-button" disabled>Submit</button>

</div>

这是我的JavaScript起点:通过Karl更新在所有输入上绑定一个 change 事件,然后使用某些条件:

Here is my javascript starting point: Updated via Karl Bind a change event on all input and then use some condition:

  $('.upload-block input').change(function() {
    $('#upload-btn').prop(
        'disabled',
        !($('.upload-block :checked').length && $('#InputFile').val()));
  });

示例

Example

这适用于所有复选框,并且 #InputFile 的值不是"none".即已选择文件的位置.但是,这在IE9中不起作用

This works for all the checkboxes and #InputFile has a value other than none. i.e. where a file has been chosen. However this does not work in IE9

我如何更新它以在IE9中使用?

How can I update this to work in IE9?

推荐答案

在所有输入上绑定一个 change 事件,然后使用某些条件:

Bind a change event on all input and then use some condition:

  $('.upload-block input').change(function() {
    $('#upload-btn').prop(
        'disabled',
        !($('.upload-block :checked').length && $('#InputFile').val()));
  });

示例

Example

这篇关于如何检查是否在&lt; input type =“文件"&gt;中选择了文件?元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆