2014-10-07 66 views
0
checkdata['attendance_batch'] = [ 
    {user_id_c: 'abcda', ispresent: 0}, 
    {user_id_c: 'abxcda', ispresent: 2}, 
    {user_id_c: 'abctda', ispresent: 1} 
] 

我希望我在上面提到的格式排列在javascript提交的二維陣列以JavaScript

MY HTML:

<label for='flip'>" + entry["name"] + "</label><div id='switch'><select name='user_id_c' id='flip2b' iduser='"+entry["id"]+"' data-role='slider'><option value='1'>Present</option><option value='0'>Absent</option></select> 


$("#frmattendance").submit(function(event) { 


event.preventDefault(); 
$form = $(this); 

var a = {}; 

var paramString = []; 
$($form).find(':input').each(function() { 

    //console.log($(this).attr('name')); 
    if ($(this).val() == 'Mark') { 
    } 

    else { 

     a[{user_id_c: $(this).attr('iduser'), ispresent: $(this).val()}]; 
     // console.log(a[$(this).attr('name')]); 
    } 

}); 

var $sessiondata = sessionStorage.sessionid; 


$.ajax 
     ({ 
      type: "POST", 
      url: saverecordurl + 'pcc_attendance', 
      dataType: 'json', 
      async: false, 
      // session_id: $sessiondata, 
      //json object to sent to the authentication url 
      data: {checkdata: a, session_id: $sessiondata}, 
      success: function(response) { 
       //console.log(response); 
       if (response.id !== null) 
       { 

        alert("You have successfully marked the attendance"); 

        $.mobile.changePage("#eventdetails", { 
         transition: "slide", 
         reverse: true, 
         changeHash: true 
        }); 

       } 

      }, 
      error: function(result) { 
       $.mobile.changePage("#one", { 
        transition: "slide", 
        reverse: false, 
        changeHash: true 
       }); 
      } 

     }) 

});

有人可以幫助我的格式是我犯了一個錯誤嗎?

+0

什麼是HTML做什麼呢? – amphetamachine 2014-10-07 19:24:34

+0

從html獲取數據並形成數組 – vini 2014-10-07 19:26:09

+1

這是一個對象數組,而不是一個二維數組。我不太清楚你在問什麼。當你說你想「提交」這些數據時,你的意思是喜歡在POST嗎? – 2014-10-07 19:26:21

回答

1

a[{user_id_c: $(this).attr('iduser'), ispresent: $(this).val()}];是無效的語法。你可以做下列之一:

var a = { 
    user_id_c: $(this).attr('iduser'), 
    ispresent: $(this).val() 
} 

var a = {} 
... 
a.user_id_c = $(this).attr('iduser'); 
a.ispresent = $(this).val() 
+0

謝謝,但不是隻追加最後一個值而不是追加所有值。我的表單上有多個具有相同名稱的元素來標記出席 – vini 2014-10-07 19:38:51

+0

'checkdata.attendance_batch.push(a);'將它們添加到您的checkdata數組 – 2014-10-07 20:22:54

+0

attendance_batch.push(a);我做了這個似乎是ovoveiting我的數據 – vini 2014-10-07 20:47:28