2013-10-15 23 views
2

我試圖使用jquery get()方法發送我的網頁的表單數據。但是,當我只提交發送到服務器的現場數據時,只有少量的表單。Ajax serialize()方法沒有讀取html表單的所有數據字段

形式:

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

$('#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字段的值。 這是後標題,後內容和標籤 我不知道爲什麼會發生這種情況。 任何幫助,將不勝感激。

+0

您是否隨時禁用表單中的任何標籤? – pythonian29033

+0

@ pythonian29033我的表單中的所有標記始終處於啓用狀態,我沒有禁用任何這些標記 – james

+3

如果輸入元素中沒有'name'屬性,'serialize'不知道如何命名它。 – Steve

回答

2

你有兩個問題。

  1. Ajax和序列化上載不適用於文件上載。 (Read this question and answer for async upload

  2. jquery form serialize需要名稱屬性。您的選擇框(form-field-select-3)沒有名稱屬性。

下面是jquery serialize文檔頁面的說明 -

注意:只有 「successful controls」 序列化到字符串。沒有 提交按鈕值被序列化,因爲表單未提交 使用按鈕。對於要包含在 序列化字符串中的表單元素的值,元素必須具有name屬性。僅當檢查框中包含 複選框和單選按鈕(「收音機」或「複選框」類型的輸入) 的值。來自文件選擇元素 的數據未被序列化。

+0

該死的你!我打字太快了! – pythonian29033

+1

Stackoverflow哥們。 :) – Mohit

1

我看到attrbute name=""是必需的,一些輸入元件缺少這些。所以你可以嘗試放置這個屬性,看看這是否解決了這個問題:

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

確定整個表單,如果所有四個元素都從比零更高的索引中填充/選擇,則只有四個元素可能會發送;
帶這些名字的人;
「標籤」
「文件格式」
「內容後」
「后冠軍」

這是因爲那些與定義的名稱屬性的唯一標籤。
請給你想要發佈到服務器的所有元素的名稱屬性,並在其中加入你想用來訪問它們的帖子索引。

3

它,因爲你已經錯過了「名稱」中選擇元素屬性

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

我在當地檢查了,現在這個是工作的罰款。

請檢查並讓我知道是否有問題。

謝謝

+0

它的工作原理,但文件不會被髮送。這是因爲serialize不支持文件(來自Mohit Jain的答案)。謝謝 – james

+0

爲此,你可以檢查這個答:[如何做文件上傳使用jQuery序列化](http://stackoverflow.com/questions/4545081/how-to-do-file-upload-使用-jquery的序列化) – Chandresh

相關問題