如果字段"file"为空,则隐藏上传提交按钮

War*_*ace 2 javascript jquery file-upload

有了这个简单的例子

<form action="" method="post" enctype="multipart/form-data">
            Upload images: <input type="file" name="images[]" multiple="multiple"> <input type="submit" name="submitImgs" value="Upload"/>
        </form>
Run Code Online (Sandbox Code Playgroud)

如何隐藏提交按钮,直到文件字段中的某些内容为止,我已经尝试制作一个PHP脚本,看看$ _FILES ['error'] == 4或is_file_uploaded等,并且不起作用.所以我想简单地隐藏按钮,直到在文件字段中选择了某些内容.

答案可能是javascript,jQuery ......我不在乎:D

谢谢

Rob*_*b W 7

<input type="file">元素具有files属性.

只需检查files.length.

jQuery示例,它可以动态修改提交按钮:

// Select the <input type="file"> element
$('input[type=file][name="images[]"]').change(function(){
    var hasNoFiles = this.files.length == 0;
    $(this).closest('form') /* Select the form element */
       .find('input[type=submit]') /* Get the submit button */
       .prop('disabled', hasNoFiles); /* Disable the button. */
});
Run Code Online (Sandbox Code Playgroud)