jQuery 在文件选择上提交表单不起作用

发布于 2024-11-02 11:38:11 字数 1663 浏览 0 评论 0原文

我试图在用户选择文件后立即启动文件上传。该表单应该消失,取而代之的是“正在上传您的图片...”消息。

到目前为止,我得到的只是隐藏的表单(和显示的消息),但上传没有发生。未提交表格。有什么想法吗?

这是 JS

<script>
$(function(){
    $("#file_select").change(function(){
        $(this).parents("#upload_form").submit();
        $("#upload_form_div").hide();
        $("#loading").show();
    });
});
</script>

和 HTML

    <div class="float_left" id="upload_form_div"> 
        <h3>Select a picture for your profile (4 MB max):</h3> 
        <form action="http://example.com/profile/upload_picture" 
            id="upload_form" 
            enctype="multipart/form-data" 
            method="post" accept-charset="utf-8">

            <input type="file" name="userfile" 
                   id="file_select" /> 
             <input type="hidden" name="upload" value="upload" /> 

         <button id="submit" type="submit"><img height="24" width="24"
                   alt="Upload" src="images/icons/small/white/bended%20arrow%20right.png"> 
                    <span>Upload</span> 
              </button> 
        </form>

        <form action="http://example.com/profile/remove_picture" 
              method="post" accept-charset="utf-8">
        <button type="submit"><img height="24" width="24" 
             alt="Remove" src="images/icons/small/white/trashcan.png"> 
            <span>Remove</span> 
        </button> 
        </form>
</div> 
  <div id="loading" style="display:none;"> 
         Uploading your picture...
   </div>

I am trying to initiate upload of a file as soon as the user selects the file. The form should disappear replaced by the "Uploading your picture..." message.

So far, all I get is the form being hidden (and message showing), but the upload is not happening. The form is not being submitted. Any ideas why?

This is the JS

<script>
$(function(){
    $("#file_select").change(function(){
        $(this).parents("#upload_form").submit();
        $("#upload_form_div").hide();
        $("#loading").show();
    });
});
</script>

and the HTML

    <div class="float_left" id="upload_form_div"> 
        <h3>Select a picture for your profile (4 MB max):</h3> 
        <form action="http://example.com/profile/upload_picture" 
            id="upload_form" 
            enctype="multipart/form-data" 
            method="post" accept-charset="utf-8">

            <input type="file" name="userfile" 
                   id="file_select" /> 
             <input type="hidden" name="upload" value="upload" /> 

         <button id="submit" type="submit"><img height="24" width="24"
                   alt="Upload" src="images/icons/small/white/bended%20arrow%20right.png"> 
                    <span>Upload</span> 
              </button> 
        </form>

        <form action="http://example.com/profile/remove_picture" 
              method="post" accept-charset="utf-8">
        <button type="submit"><img height="24" width="24" 
             alt="Remove" src="images/icons/small/white/trashcan.png"> 
            <span>Remove</span> 
        </button> 
        </form>
</div> 
  <div id="loading" style="display:none;"> 
         Uploading your picture...
   </div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖微凉心微凉 2024-11-09 11:38:11

它可能与这部分有关:

 <input type="file" name="userfile" 
               value="onchange="return validateFileExtension(this)""
               id="file_select" /> 

输入类型=文件不应该有值=属性(即使有,你也不应该把javascript放在那里!)

你还有一些表单中的javascript:

onsubmit="return validateFileExtension(this.userfile)" 

如果函数 validateFileExtension() 返回 false 那么表单将不会提交。
但是,您编写 jQuery 的方式意味着该消息仍然会出现。


编辑:

将提交按钮上的 ID 更改为“提交”以外的其他内容。

在 jQuery 文档中:

表单及其子元素不应使用与表单属性(例如提交、长度或方法)冲突的输入名称或 ID。名称冲突可能会导致混乱的故障。有关规则的完整列表以及检查标记是否存在这些问题,请参阅 DOMLint


但是:

您应该考虑@Abdul 的解决方案,因为工作提交将使您远离您的消息。

如果您正在使用 CodeIgniter 并且不想使用 Ajax,则应考虑使用 flash 功能在表单提交后向用户显示消息。

It probably has something to do with this part:

 <input type="file" name="userfile" 
               value="onchange="return validateFileExtension(this)""
               id="file_select" /> 

An input type=file should not have a value= attribute (and even if it did, you shouldn't put javascript in there!)

You also have some javascript in the form:

onsubmit="return validateFileExtension(this.userfile)" 

If the function validateFileExtension() returns false then the form will not submit.
However, the way you have written the jQuery means that the message will still appear.


EDIT:

Change the id on your submit button to something other than "submit".

In the jQuery docs:

Forms and their child elements should not use input names or ids that conflict with properties of a form, such as submit, length, or method. Name conflicts can cause confusing failures. For a complete list of rules and to check your markup for these problems, see DOMLint.


HOWEVER:

You should consider @Abdul's solution, since the working submit will take you away from your message.

If you are using CodeIgniter and you don't want to use Ajax, you should consider using the flash functionality to display the message to the user after the form submits.

假装不在乎 2024-11-09 11:38:11

您应该考虑使用 jquery 表单插件 提交表单并 jquery 验证插件 用于验证表单的文件扩展名和所有内容。Jquery 表单插件使用 ajax 提交表单。这样您就不会被重定向到表单操作。还
如果你愿意,你可以考虑使用 jquery 对话框来显示结果。

 $("#upload_form").validate({
          rules: {
        MyFile: {
          required: false,
          accept: "jpg|jpeg|png|gif"

        }
      }
    });


  $("#file_select").change(function(){
       ("#upload_form").ajaxSubmit();
        $("#upload_form_div").hide();
        $("#loading").show();
    });

You should think of using jquery form plugin to submit your form and jquery validate plugin to validate your form for file extensions and everything.Jquery form plugin submits your form using ajax. In that way you won't be redirected to form action. Also
if you want you can consider using jquery dialog to display the result.

 $("#upload_form").validate({
          rules: {
        MyFile: {
          required: false,
          accept: "jpg|jpeg|png|gif"

        }
      }
    });


  $("#file_select").change(function(){
       ("#upload_form").ajaxSubmit();
        $("#upload_form_div").hide();
        $("#loading").show();
    });
月下凄凉 2024-11-09 11:38:11

提交文件选择表单并验证 CSV 文件输入

 $('#file').change($('form').submit());
 $('form').submit(function(){
    if($('#file').val().toLowerCase().indexOf('.csv') != -1){
      $('#spinner').show();
      return true;
    }
    else{
      alert('Invalid File format. Please, upload CSV file')
      return false;
    }
  });

Submit form on file selection and validate for CSV file input

 $('#file').change($('form').submit());
 $('form').submit(function(){
    if($('#file').val().toLowerCase().indexOf('.csv') != -1){
      $('#spinner').show();
      return true;
    }
    else{
      alert('Invalid File format. Please, upload CSV file')
      return false;
    }
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文