jquery:如何在 jquery 中序列化文件类型输入

发布于 2024-09-10 17:28:46 字数 3144 浏览 1 评论 0原文

我想序列化输入类型 file 中上传的文件的文件名,但是当我在 jquery 中序列化表单时,我只得到输入类型文本,而其他不是文件 - 我该怎么做?这是jquery无法实现的东西吗?

这是 html/php:

<form action="<?php echo HTTP_ROOT.INC_LAYOUT;?>ask_add_xml.php" method="post" enctype="multipart/form-data" id="form_qna_ask">

        <div class="item-form">
            <h2><a href="#"><span>ASK</span></a></h2>
            <label id="ask_title_label">
            <input type="text" name="ask_title" id="ask_title" value="" title="TYPE IN A TITLE"/>
            </label>
        </div>

        <div class="item-form">
            <h2><a href="#"><span>EMAIL</span></a></h2>
            <label id="ask_email_label">
            <input type="text" name="ask_email" id="ask_email" value="" title="TYPE IN YOUR EMAIL"/>
            </label>
        </div>

        <div class="item-form">
            <label id="ask_content_label">
                <textarea name="ask_content" id="content_mce" title="CONTENT"></textarea>
            </label>
        </div>

        <div class="item-form">
            <div class="left">
                <label>
                <img src="views/www/styles/images/global/button-add-images-q-n-a.gif" alt="add images" style="float:left;"/> 
                <h2 id="add_images_label"><a href="#"><span>Add Images</span></a></h2>
                </label>
            </div>

            <div class="right">
                <div class="processing"></div>
                <input type="submit" name="cancel" value="CANCEL"/>
                <input type="submit" name="submit" value="SUBMIT"/>
            </div>
        </div>


        <div class="item-form" style="border:1px solid #bbbbbb; padding:10px 10px 20px 15px; background-color:#ffffff;">
            <p>Add images: this allows you to attach pictures to your question. Only 2 pictures at 2MB each are allowed to upload.</p>
            <input type="file" name="image[]"/>
            <input type="file" name="image[]" />
        </div>

    </form>

这是 jquery,

this.postForm_ask = function(){
$("#form_qna_ask").submit(function(){
    $('#popup_result').remove();
    var path = $(this).attr('action');
    var processing = $('#q-n-a .processing');
    processing
        .css({
            margin:"0px 0px 0px -80px",
            position:"absolute",
            visibility:"visible"
            });

    processing.html('<div><p><img src="'+http_root+img_global+'loader-2b.gif"/> loading</p></div>');
    $.post(path, $("#form_qna_ask").serialize(),function(xml){
            alert($("#form_qna_ask").serialize());
            processing
                .css({
                    visibility:"hidden"
                    });
            processAsk(xml);
        });
    return false;
    });
}

非常感谢, 刘

I want to serialize file name of the file being uploaded in the input type file , but when I serialized the form in jquery I just got the input type text and others not the file - how do I do it? is it something jquery fails to achieve??

this is the html/php:

<form action="<?php echo HTTP_ROOT.INC_LAYOUT;?>ask_add_xml.php" method="post" enctype="multipart/form-data" id="form_qna_ask">

        <div class="item-form">
            <h2><a href="#"><span>ASK</span></a></h2>
            <label id="ask_title_label">
            <input type="text" name="ask_title" id="ask_title" value="" title="TYPE IN A TITLE"/>
            </label>
        </div>

        <div class="item-form">
            <h2><a href="#"><span>EMAIL</span></a></h2>
            <label id="ask_email_label">
            <input type="text" name="ask_email" id="ask_email" value="" title="TYPE IN YOUR EMAIL"/>
            </label>
        </div>

        <div class="item-form">
            <label id="ask_content_label">
                <textarea name="ask_content" id="content_mce" title="CONTENT"></textarea>
            </label>
        </div>

        <div class="item-form">
            <div class="left">
                <label>
                <img src="views/www/styles/images/global/button-add-images-q-n-a.gif" alt="add images" style="float:left;"/> 
                <h2 id="add_images_label"><a href="#"><span>Add Images</span></a></h2>
                </label>
            </div>

            <div class="right">
                <div class="processing"></div>
                <input type="submit" name="cancel" value="CANCEL"/>
                <input type="submit" name="submit" value="SUBMIT"/>
            </div>
        </div>


        <div class="item-form" style="border:1px solid #bbbbbb; padding:10px 10px 20px 15px; background-color:#ffffff;">
            <p>Add images: this allows you to attach pictures to your question. Only 2 pictures at 2MB each are allowed to upload.</p>
            <input type="file" name="image[]"/>
            <input type="file" name="image[]" />
        </div>

    </form>

this is the jquery,

this.postForm_ask = function(){
$("#form_qna_ask").submit(function(){
    $('#popup_result').remove();
    var path = $(this).attr('action');
    var processing = $('#q-n-a .processing');
    processing
        .css({
            margin:"0px 0px 0px -80px",
            position:"absolute",
            visibility:"visible"
            });

    processing.html('<div><p><img src="'+http_root+img_global+'loader-2b.gif"/> loading</p></div>');
    $.post(path, $("#form_qna_ask").serialize(),function(xml){
            alert($("#form_qna_ask").serialize());
            processing
                .css({
                    visibility:"hidden"
                    });
            processAsk(xml);
        });
    return false;
    });
}

many thanks,
Lau

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

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

发布评论

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

评论(2

落墨 2024-09-17 17:28:46

谢谢你们。刚刚用下面的插件整理好了!

http://malsup.com/jquery/form/#getting-started

最终代码,

this.postForm_ask = function(){

 $('#form_qna_ask').submit(function() { 
  var processing = $('#q-n-a .processing');
  processing
   .css({
    margin:"0px 0px 0px -80px",
    position:"absolute",
    visibility:"visible"
    });
  processing.html('<div><p><img src="'+http_root+img_global+'loader-2b.gif"/> loading</p></div>');
  $(this).ajaxSubmit({ 
   target: '#output',
   // dataType identifies the expected content type of the server response 
   dataType:  'xml', 

   // success identifies the function to invoke when the server response 
   // has been received 
   success: processXML_ask 
  }); 
        return false; 
 });

}

this.processXML_ask = function(xml){ //  ==  function addMessages(xml) {  

 var processing = $('#q-n-a .processing');
 processing.css({
     visibility:"hidden"
     });

 $(document.body).append("<div id=\"popup_result\" class=\"popup\"></div>");
 var target = $('#popup_result');
 var scrollTop = $(window).scrollTop();
 var scrollLeft = $(window).scrollLeft();
 var width = 400;
 var top = 200;
 var marginLeft = "-"+ ((scrollLeft + width)/2);
 target
  .css({
   top:(scrollTop + top) + "px", 
   left:"50%",
   marginLeft:marginLeft + "px",
   width:width + "px",
   zIndex:"11",
   display:"none"
   });

 target.load(http_root+inc_layout+"result.php", {}, function(){
 $("error", xml).each(function(){
  var elementid = $(this).attr('elementid');
  var message = $(this).attr('message');
  //alert(elementid);
  $("#"+elementid+"_label").addClass('error-qna');
  $(".result").append("<img src='"+http_root+img_global+"attention.png' /> <b>" + message + "</b> <br />");
   target.fadeIn('slow', function(){ 
    closePopup(target);
    //$('form *[title]').inputHint();
   }); 
  });
 $("result", xml).each(function(){
  var message = $(this).attr('message');
  $(".result").append("<img src='"+http_root+img_global+"info.png' /> <b>" + message + "</b> <br />");
   target.fadeIn('slow', function(){ 
    closePopup(target);
    clearFormElements('form');
    $('.form *[title]').inputHint();
    $('input:file').MultiFile('reset');
   }); 
  }); 
 });
}

thanks guys. just got it sorted with plug in below!

http://malsup.com/jquery/form/#getting-started

final code,

this.postForm_ask = function(){

 $('#form_qna_ask').submit(function() { 
  var processing = $('#q-n-a .processing');
  processing
   .css({
    margin:"0px 0px 0px -80px",
    position:"absolute",
    visibility:"visible"
    });
  processing.html('<div><p><img src="'+http_root+img_global+'loader-2b.gif"/> loading</p></div>');
  $(this).ajaxSubmit({ 
   target: '#output',
   // dataType identifies the expected content type of the server response 
   dataType:  'xml', 

   // success identifies the function to invoke when the server response 
   // has been received 
   success: processXML_ask 
  }); 
        return false; 
 });

}

this.processXML_ask = function(xml){ //  ==  function addMessages(xml) {  

 var processing = $('#q-n-a .processing');
 processing.css({
     visibility:"hidden"
     });

 $(document.body).append("<div id=\"popup_result\" class=\"popup\"></div>");
 var target = $('#popup_result');
 var scrollTop = $(window).scrollTop();
 var scrollLeft = $(window).scrollLeft();
 var width = 400;
 var top = 200;
 var marginLeft = "-"+ ((scrollLeft + width)/2);
 target
  .css({
   top:(scrollTop + top) + "px", 
   left:"50%",
   marginLeft:marginLeft + "px",
   width:width + "px",
   zIndex:"11",
   display:"none"
   });

 target.load(http_root+inc_layout+"result.php", {}, function(){
 $("error", xml).each(function(){
  var elementid = $(this).attr('elementid');
  var message = $(this).attr('message');
  //alert(elementid);
  $("#"+elementid+"_label").addClass('error-qna');
  $(".result").append("<img src='"+http_root+img_global+"attention.png' /> <b>" + message + "</b> <br />");
   target.fadeIn('slow', function(){ 
    closePopup(target);
    //$('form *[title]').inputHint();
   }); 
  });
 $("result", xml).each(function(){
  var message = $(this).attr('message');
  $(".result").append("<img src='"+http_root+img_global+"info.png' /> <b>" + message + "</b> <br />");
   target.fadeIn('slow', function(){ 
    closePopup(target);
    clearFormElements('form');
    $('.form *[title]').inputHint();
    $('input:file').MultiFile('reset');
   }); 
  }); 
 });
}
予囚 2024-09-17 17:28:46

您确定要做这个服务器端吗?该文件以二进制形式发送,因此无论如何都无法通过 XHR 发送。

如果您想要即时文件上传,请尝试使用 iframe。

Surely you want to do this server side? The file is sent in binary so it cannot be sent via XHR anyway.

If you want instant file uploads try using an iframe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文