【问题标题】:Hide the upload submit button if field "file" is empty如果“文件”字段为空,则隐藏上传提交按钮
【发布时间】:2012-03-09 14:23:53
【问题描述】:

通过这个简单的例子

<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>

如何隐藏提交按钮,直到文件字段中出现某些内容,我尝试制作一个 php 脚本来查看 $_FILES['error'] == 4 或 is_file_uploaded 等是否不起作用.所以我想简单地隐藏按钮,直到在文件字段中选择某些内容。

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

谢谢

【问题讨论】:

    标签: javascript jquery file-upload


    【解决方案1】:

    &lt;input type="file"&gt; 元素有一个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. */
    });
    

    【讨论】:

    • 是否应该在开始时禁用提交按钮??
    • @Warface 是的,这可以通过将disabled 添加到您的提交按钮来完成。
    【解决方案2】:

    使用这个。附上文件字段change事件的代码。

      $(function() {
         $("input:file").change(function() {
           var fileName = $(this).val();
           if(filename != "") { $("#submitButtonId").show(); } //show the button
         });
      });
    

    【讨论】:

      【解决方案3】:

      提交的默认状态应该是隐藏的(CSS display: none) 在文件字段的更改事件中,您会显示提交按钮

      她的密码

      <script>
      $(function() {
        $("#myfileinput").change(function() {
          if($(this)).val() != "") $("submit").show();
        });
      });
      </script/>
      <form action="" method="post" enctype="multipart/form-data">
      Upload images: <input id="myfileinput" type="file" name="images[]" multiple="multiple">
      <input type="submit" name="submitImgs" value="Upload" style="display:none"/>
      </form>
      

      【讨论】:

        【解决方案4】:
        <script>
        function checkFile() {
            var myFSO = new ActiveXObject("Scripting.FileSystemObject");
            var filepath = document.upload.file.value;
            var thefile = myFSO.getFile(filepath);
            var size = thefile.size;
            if(size > 0) {
                document.getElementById('submit').style.display='block';
            }
        }
        </script>
        <form action="" method="post" enctype="multipart/form-data">
                    Upload images: <input type="file" onChange="checkFile()" name="images[]" multiple="multiple"> <input type="submit" style="display:none;" name="submitImgs" value="Upload" id="submit" />
        </form>
        

        【讨论】:

        • ActiveXObject 是一个专有的 Micro$oft 浏览器扩展。此代码仅有时在(旧版本的)IE 中工作。此外,getElementById 是一个函数,而不是一个属性。使用括号而不是括号。
        猜你喜欢
        • 2020-09-15
        • 2023-03-26
        • 2020-11-06
        • 1970-01-01
        • 2013-10-02
        • 1970-01-01
        • 2021-11-21
        • 2016-03-06
        • 1970-01-01
        相关资源
        最近更新 更多