2012-04-06 3 views
1

페이지에서 목록을 선택하고 모든 선택 목록을 반복합니다. 값은 "default", "Excuses Absent"및 "Excused Late"입니다. 기본값은 기본적으로 "선택 ..."입니다. 나는 이것을 서버에 전달하거나 아무런 처리가 필요 없기 때문에 무의미하다.왜이 배열에 NULL 값을 가져 옵니까?

이 내 jQuery를 : 그것은이 경우, 3 번 배의 정확한 금액 내가 그것을 테스트 할 때마다 거짓 인쇄하기 때문에

attendSelect.each(function(k, v) 
    { 
     attendance = $(this).val(); 

     if(attendance != "default") 
     { 
      console.log(attendance == "default"); 
      students[k] = 
      { 
       lesson : $(this).attr('id'), 
       student_id : $(this).attr('name'), 
       attendance : attendance 
      }; 
     }  
    }); 

이 작동합니다. 그러나 문제는 서버 측에 있습니다 (제 생각에는?). 변수를 출력 할 때 jQuery에서 기본값을 찾은 시간에 NULL이 NULL로 표시됩니다. 확실히, 나는 NULLs가없는 크기 3의 배열만을 가져야한다. 내가 널 (null)을받을 이유를 문 경우에도 입력하지 않는 경우

students = JSON.stringify(students) 

    if(attendSelect.length)//protect against submitting on past lessons 
    { 
     $.post('', { students : students, cid: cid }, function(response) 
     { 

      console.log(response);   
     }); 
    } 

가 이해가 안 : 이것은 내 AJAX이다

$students = json_decode($_POST['students'], true); 
var_dump($students); 

array(12) { 
    [0]=> 
    NULL 
    [1]=> 
    NULL 
    [2]=> 
    NULL 
    [3]=> 
    array(3) { 
    ["lesson"]=> 
    string(9) "lesson[7]" 
    ["student_id"]=> 
    string(12) "student[241]" 
    ["attendance"]=> 
    string(14) "Excused Absent" 
    } 
    [4]=> 
    array(3) { 
    ["lesson"]=> 
    string(9) "lesson[7]" 
    ["student_id"]=> 
    string(12) "student[270]" 
    ["attendance"]=> 
    string(12) "Excused Late" 
    } 
    [5]=> 
    NULL 
    [6]=> 
    NULL 
    [7]=> 
    NULL 
    [8]=> 
    NULL 
    [9]=> 
    NULL 
    [10]=> 
    NULL 
    [11]=> 
    array(3) { 
    ["lesson"]=> 
    string(9) "lesson[9]" 
    ["student_id"]=> 
    string(12) "student[317]" 
    ["attendance"]=> 
    string(14) "Excused Absent" 
    } 
} 

:

는 PHP에서 무엇을 인쇄한다 jQuery에서.

답변

1

의 문제가이이 라인에 대신

students[k] = 

, 당신은 .push()

students.push(
     { 
      lesson : $(this).attr('id'), 
      student_id : $(this).attr('name'), 
      attendance : attendance 
     }); 

귀하의 k 값이 attendSelect 당신이 처리하는의 인덱스를 사용해야합니다. students 배열을 만들 때 새로운 배열을 만드는 대신 이러한 인덱스 키를 할당하게됩니다. Javascript는 누락 된 인덱스를 NULL 값으로 채 웁니다.

+0

로 분류됩니다. 감사 :) –

1

JSON의 배열은 인덱스를 건너 뛸 수 없습니다.

array_filter (두 번째 인수로 아무것도 전달하지 않음)을 사용하여 null 값을 필터링 할 수 있습니다.

관련 문제