首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax序列化()方法没有读取html表单的所有数据字段

Ajax序列化()方法没有读取html表单的所有数据字段
EN

Stack Overflow用户
提问于 2013-10-15 09:19:06
回答 4查看 7.7K关注 0票数 3

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

表格:

代码语言:javascript
复制
  <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:

代码语言:javascript
复制
 $('#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个字段值被发送到服务器。那是帖子标题,帖子内容和标签我不知道为什么会这样。任何帮助都将不胜感激。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-10-15 09:27:40

你有两个问题。

  1. Ajax和序列化上传不适用于文件上载。(阅读此问题和答案以获取异步上载。)
  2. jquery表单序列化需要一个name属性。您的复选框(form-field-select-3)没有名称属性。

以下是jquery序列化文档页面中的注释-

注意:只有"成功控制“被序列化到字符串。没有序列化submit按钮值,因为表单没有使用按钮提交。要使表单元素的值包含在序列化的字符串中,元素必须具有name属性。复选框和单选按钮(输入“单选”或“复选框”)中的值只有在选中时才会包含。没有序列化来自文件select元素的数据。

票数 4
EN

Stack Overflow用户

发布于 2013-10-15 09:32:15

这是因为您错过了select元素中的"name“属性。

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

我已经在当地住过了,现在一切都很顺利。

如果有什么问题,请查收并通知我。

谢谢

票数 4
EN

Stack Overflow用户

发布于 2013-10-15 09:26:28

我看到,吸引人的name=""是必需的,而一些输入精灵缺少这些。因此,您可以尝试放置这个属性,看看它是否解决了这个问题:

代码语言:javascript
复制
 <select class="chzn-select" name="your-elem-name">
 //--------------------------^^^^^^^^^^^^^^^^^^^^^-----try placing the name attr
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19377487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档