2012-05-08 23 views
2

我一直在努力與datatables和如何處理fnGetNodes正確推送數據回到表單上提交。Datatables - fnGetNodes()如何將結果推送回服務器

我有jquery正常工作。我可以看到documentation中所述的選定值。我的問題是我如何採取sData並將其推回POST中的服務器?

我知道這一定很簡單,但我明顯太專注於樹上看到森林..我會很感激任何幫助!

<script style="text/javascript"> 

    $(document).ready(function() { 
     $('#form').submit(function() { 
      var sData = $('input', oTable.fnGetNodes()).serialize(); 
      alert("The following data would have been submitted to the server: \n\n"+sData); 
      return false; 
     }); 

     oTable = $('#data_table').dataTable(); 
    }); 
</script> 

我的HTML表單看起來像這樣(縮短清晰度)

<table id="data_table"> 
      <thead> 
       <tr> 
        <th>Select</th> 
        <th>Question</th> 
       </tr> 
      </thead> 
      <tr id=0><td><label for="id_questions_0"><input type="checkbox" name="questions" value="103" id="id_questions_0" /></label></td><td>D1. Example of a multiple choice question</td></tr> 
<tr id=1><td><label for="id_questions_1"><input type="checkbox" name="questions" value="104" id="id_questions_1" /></label></td><td>E1. Example of a multiple choice question</td></tr> 
<tr id=2><td><label for="id_questions_2"><input type="checkbox" name="questions" value="105" id="id_questions_2" /></label></td><td>G. Example of a multiple choice question</td></tr> 
<tr id=3><td><label for="id_questions_3"><input type="checkbox" name="questions" value="106" id="id_questions_3" /></label></td><td>H. Example of a multiple choice question</td></tr> 

編輯

警報是向我展示這一點。 questions=103&questions=104&questions=105&questions=106&questions=100&questions=101&questions=102

和後(使用開發工具(是我看這個。

csrfmiddlewaretoken:a2c3ed6e1bfee9fce0b7412553aa2080 
name:Phase-1 Pre-Drywall 
priority:1 
description:Pre-Drywall inspection items 
use_for_confirmed_rating:on 
use_for_sampling:on 
data_table_length:10 
questions:103 
questions:104 
questions:105 
questions:106 
submit:Submit 

所以不知何故,我需要前者轉變爲後來的使用jQuery 有人可以幫我這個

感謝

回答

3

答案原來(如預期)是非常簡單的。

 var oTable = $('#data_table').dataTable(); 
     // This will collect all of the nodes which were checked and make sure they get 
     // pushed back. 
     $('#form').submit(function() { 
      $("input[name='question']").remove(); //Remove the old values 
      $("input:checked", oTable.fnGetNodes()).each(function(){ 
       $('<input type="checkbox" name="questions" ' + 'value="' + 
        $(this).val() + '" type="hidden" checked="checked" />') 
        .css("display", "none") 
        .appendTo('#form'); 
      }); 
     });