表单提交不提交文件类型输入

时间:2016-08-03 03:06:36

标签: jquery forms

我动态地将表单附加到正文:

表格是:

    <form action="" data-reply-form class="ckit-composer-form" enctype="multipart/form-data">'
   +'<div class="ckit-composer">'
        +'<textarea data-ckit-composer-textarea placeholder="Add your reply" autocomplete="off" name="message" class="form-control ckit-composer__textarea"></textarea>'
        +'<input type="file" name="fileToUpload" data-fileToUpload multiple>'
        +'</div>'
        +'<input type="hidden" name="c_id" value="">'     
    +'</form>

我以这种方式触发提交:

$('[data-fileToUpload]').change( function(event) {

      $('[data-reply-form]').submit();
});

在另一页上:

$("[data-reply-form]").submit(function(){
        var data = $(this).serialize();
        console.debug(data);
    });

但它只会注销,消息和c_id输入..为什么它不显示文件输入值?如何获取文件输入值,如名称,大小,tmp_name等?

3 个答案:

答案 0 :(得分:0)

带上传的表单应使用POST方法发送

尝试使用此表单标记

<form action="" data-reply-form class="ckit-composer-form" enctype="multipart/form-data" method="POST">'

您也可以使用

访问文件信息

$ _文件

http://php.net/manual/en/reserved.variables.files.php

示例:$ _FILES [&#39; fileToUpload&#39;] [&#39; tmp_name&#39;]

将为您提供通过HTTP POST

发送的文件的临时文件名

答案 1 :(得分:0)

检查一下:

                <html>
            <head>
            <script
                src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            </head>
            <body>
                <form action="demo_check.php" data-reply-form
                    class="ckit-composer-form" enctype="multipart/form-data" method="POST">
                    <div class="ckit-composer">
                        <textarea data-ckit-composer-textarea placeholder="Add your reply"
                            autocomplete="off" name="message"
                            class="form-control ckit-composer__textarea"></textarea>
                        <input type="file" name="fileToUpload" id="fileinput"
                            data-fileToUpload multiple>
                    </div>
                    <input type="hidden" name="c_id" value="">
                </form>
                <script type="text/javascript">
                            $('[data-fileToUpload]').change( function(event) {

                                  $('[data-reply-form]').submit();
                            });

                            $("[data-reply-form]").submit(function(){
                                 var name = $('input[type=file]')[0].files[0].name;
                                 alert(name);
                                 var size = $('input[type=file]')[0].files[0].size;
                                 alert("size: "+size);
                        });
                    </script>
            </body>
            </html>

答案 2 :(得分:0)

检查一下:

            <html>
        <head>
        <script type="text/javascript" src="js/jquery-1.12.1.js"></script>
        </head>
        <body>
            <input type="file" id="i_file" value="">
            <input type="button" id="i_submit" value="Submit">
            <br>
            <img src="" width="200" style="display: none;" />
            <br>
            <div id="disp_tmp_path"></div>


        <script type="text/javascript">
                $('#i_file').change( function(event) {
                    var tmppath = URL.createObjectURL(event.target.files[0]);
                        $("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));

                        $("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");
                });
        </script>
        </body>

        </html>