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

标签 javascript php jquery ajax forms

我正在尝试使用 jquery get() 方法发送网页的表单数据。但是当我提交表单时,只有很少的字段数据发送到服务器。

表格:

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

Java脚本:

 $('#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();
}
 )};

其中只有 3 个字段值发送到服务器。 即帖子标题、帖子内容和标签 我不知道为什么会发生这种情况。 任何帮助将不胜感激。

最佳答案

您有两个问题。

  1. Ajax 和序列化上传不适用于文件上传。 (Read this question and answer for async upload)

  2. jquery form serialize需要一个名称属性。您的选择框 (form-field-select-3) 没有 name 属性。

以下是 jquery serialize 中的注释文档页面 -

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. Values from checkboxes and radio buttons (inputs of type "radio" or "checkbox") are included only if they are checked. Data from file select elements is not serialized.

关于javascript - Ajax serialize() 方法没有读取 html 表单的所有数据字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19377487/

相关文章:

php - 在 PHP 中调用 LLVM-GCC

php - Symfony2 从 phpunit 测试中的 Twig 模板访问全局变量

javascript - 删除监听器类不会停止与 Jquery Javascript 关联的声音事件

javascript - 为什么我的 super 测试调用不会链式?

javascript - map 框 gl js : use intermediate rendering step as mask for custom layer

php - 如何使用group by获取MySql中的特定部分?

javascript - 在 mouseup 上获取元素下方的 div 目标

jquery - 在调整窗口大小之前,砌体无法工作

javascript - 更改 p 标签中单行的颜色,然后将其改回

javascript - 设置全局变量会阻止简单程序正常工作?