简体   繁体   English

Ajax serialize()方法未读取html表单的所有数据字段

[英]Ajax serialize() method is not reading all data fields of html form

I'm trying to send the form data of my web page using jquery get() method. 我正在尝试使用jquery get()方法发送我的网页的表单数据。 But when I submit the form only few of the field data where sent to the server. 但是,当我提交表单时,只有很少的字段数据发送到服务器。

Form: 形成:

  <form class="form-horizontal" id="addpost" method="GET" action="">
        <div class="control-group">
            <label class="control-label" for="form-field">Post Title</label>
            <div class="controls">
                <input type="text" id="form-field" placeholder="Post Title" name="Post-title" value="" />
             </div>
         </div>
         <div class="control-group">
         <label class="control-label" for="form-field-11">Content Here</label>
            <div class="controls">
            <textarea name="post-content"  value="" class="autosize-transition span12" id="form-field-11" style="overflow: hidden; word-wrap: break-word; resize: horizontal; height: 67px;"></textarea>
            </div>
           </div><!-- Insert Image Code -->
           <div class="control-group">
            <div class="widget-main">
                <div class="controls">
                    <div class="ace-file-input">
                        <input id="id-input-file-2" type="file">
                        <a class="remove" href="#"></a>
                    </div>
                 </div>
                 <div class="controls">
                    <div class="ace-file-input ace-file-multiple">
                        <input id="id-input-file-3" type="file" multiple="">
                        <a class="remove" href="#">
                        <i class="icon-remove"></i>
                        </a>
                    </div>
                    <label>
                    <input id="id-file-format" type="checkbox" name="file-format">
                        <span class="lbl"> Allow only images</span>
                    </label>
                </div>
              </div>
             </div><!-- Insert Image Code -->
            <div class="space-4"></div>
            <div class="control-group">
                <label class="control-label" for="form-field-tags">Tag input</label>
                <div class="controls">
                    <input id="form-field-tags" type="hidden" placeholder="Enter tags ..." value="Tag Input Control" name="tags">
                </div>
            </div>

            <div class="space-4"></div>
            <div class="control-group">
                <label class="control-label" for="form-field-select-3">Select Category</label>
                <div class="controls">
                <label for="form-field-select-3">Chosen</label>
                <select class="chzn-select" id="form-field-select-3" data-placeholder="Choose a Category...">
                <option value="">
                </option><option value="Blog">Blog
                </option><option value="News Letter">News Letter
                </option></select>
             </div>
           </div>

     <div class="control-group" style="float:left; margin-right:25px">
        <div class="controls"><button type="submit" class="btn btn-info">
        <i class="icon-ok bigger-110"></i>
        <input type="submit" value="" id="posubmit" style="opacity:0"/>Submit</button>
        <button type="reset" class="btn"><i class="icon-undo bigger-110"></i>Reset</button>
        </div>
     </div> 
     <div id="resp" style="float:left; margin-top:5px">
        <img id="loading" style="visibility:hidden;" src="assets/img/ajax-load.gif" width="16" height="16" alt="loading" />
      </div>
     </form>

JavaSccript: JavaSccript:

 $('#addpost').submit(function(e){ 
 if(use_ajax)
        {
            $('#loading').css('visibility','visible');
            $.get('test.php',$(this).serialize(),

                function(data){
                    if(parseInt(data)==-1)
                        $.validationEngine.buildPrompt("#resp","* Please ensure all fields are filled.","error");

                    else
                    {
                        $("#resp").show('slow').after('<p id="resp-mes" style=" color:#000000; text-decoration: bold;">Success....</p>');
                    }

                    $('#loading').css('visibility','hidden');
                     setTimeout( "jQuery('#resp').hide('slow');",3000 );
                     setTimeout( "jQuery('#resp-mes').hide('slow');",5000 );
                    });
        }
        e.preventDefault();
}
 )};

In this only 3 field values where sent to server. 在此只有3个字段值发送到服务器。 That is Post-title, post-content and tags I don't know why this happening. 那就是帖子标题,帖子内容和标签,我不知道为什么会这样。 Any help would be appreciated. 任何帮助,将不胜感激。

Its because you have missed "name" attribute in select element 这是因为您错过了选择元素中的“名称”属性

<select class="chzn-select" id="form-field-select-3" name="form-field-select-3" data-placeholder="Choose a Category...">

I have checked in my local, and now this is working fine. 我已经在当地检查了,现在这个工作正常。

Please check and let me know if any issue. 请检查,并让我知道是否有任何问题。

Thanks 谢谢

you have two issues. 你有两个问题。

  1. Ajax and serialize upload doesn't work with file upload. Ajax和序列化上传不适用于文件上传。 ( Read this question and answer for async upload ) 阅读此问题和答案以进行异步上传

  2. jquery form serialize needs a name attribute. jQuery表单序列化需要一个name属性。 your select box ( form-field-select-3 ) doesn't have a name attribute. 您的选择框( form-field-select-3 )没有名称属性。

following is a note in jquery serialize documentation page - 以下是jquery序列化文档页面中的注释-

Note: Only " successful controls " are serialized to the string. 注意:只有“ 成功控件 ”被序列化为字符串。 No submit button value is serialized since the form was not submitted using a button. 没有提交按钮值被序列化,因为表单未使用按钮提交。 For a form element's value to be included in the serialized string, the element must have a name attribute. 为了使表单元素的值包含在序列化的字符串中,该元素必须具有name属性。 Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. 复选框和单选按钮(“radio”或“checkbox”类型的输入)中的值仅在选中时才包括在内。 Data from file select elements is not serialized. 文件选择元素中的数据未序列化。

I see that attrbute name="" is required and some of the input elems are missing those. 我看到attrbute name=""是必需的,并且某些输入元素缺少这些元素。 so you can try placing this attribute and see if this solves the issue: 所以你可以尝试放置这个属性,看看这是否解决了这个问题:

 <select class="chzn-select" name="your-elem-name">
 //--------------------------^^^^^^^^^^^^^^^^^^^^^-----try placing the name attr

ok of this entire form, only four elements may get sent through if all four are populated/selected from a higher index than zero; 在整个表格中,如果所有四个元素都是从比零更高的索引中填充/选择的,则只有四个元素可以发送;
the ones with these names; 有这些名字的人;
"tags" “标签”
"file-format" “文件格式”
"post-content" “内容后”
"Post-title" “帖子标题”

this is because those are the only tags with a name attribute defined. 这是因为这些是唯一定义了name属性的标签。
please give all the elements you want to post through to the server a name attribute with the post index you want to use to access them with. 请为您要发布到服务器的所有元素提供一个name属性,以及您要用来访问它们的发布索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM