2011-05-03 2 views
1

이 해결책에 대한 간단한 대답이 있기 전에 이미 또는 이미 질문되었을 수 있지만 현재 멈추었습니다. 이 양식은 http://jsfiddle.net/DS73u/에서 볼 수 있습니다. 사용자는 고유 한 이름으로 3 가지 다른 방법으로 좌표를 입력 할 수 있습니다. 사용자가 양식에 입력을 추가 할 수 있도록 둘 이상의 입구가있을 수도 있습니다. 나는 체크 된 모든 체크 박스 입력을 반복하고 배열 2D 배열에 값을 저장합니다. 그것이 어떤 계획을 저장하고 좌표 값을 저장하는지. 따라서 사용자는 배열에 저장되는 55.67484와 -86.7685를 입력하고 temp [0] = 55.67484 AND temp [0] [1] = -86.7685로 액세스 할 수 있습니다.jquery를 사용하여 루프에서 onchange를 감지하는 방법

그 후에는 입구를 기준으로 각 좌표 그룹을 구분하고 싶습니다. 나는 개체 배열에 2D 배열을 저장합니다. 여기서 ent_name은 속성 이름입니다. 입구 이름을 기준으로 값을 나누는 데 문제가 있지만 모든 값이 루프 내에 포함되어 있기 때문에이를 비교하는 방법을 모르겠습니다. 어쩌면 onchange() 또는 change()가 jquery에서 작동할까요? 질문 가장, 어디 당신의 정보를 저장하는 방법에 대해 당신이있는 것처럼 텍스트 필드의 값은 지금까지

$(function() { 

    /* 
     This is where the magic happens when you click 
     the "Preview Map" button 
     It does several things... 

     1.Loops through every checkbox in the #entrances 
     2.Check to see if Entrance name has changed and store it into object with array of coordinates 
     2.Checks to see if the checkbox is checked 
      a.Yes 
       1.We see what checkbox is being checked 
       to determine how we want to output the data 
       inputted(Hence the switch) 
       2.We grab the data from the form and store it 
       into an array called coordinates 
      b.No 
       1.Do nothing 
    */ 

    $('#prev_map').click(function() { 

     //Make sure coordinates is empty before proceeding 
     coordinates = {}; 
     temp = []; 
     var ent_name; 

     //console.log($('#entrances input:checkbox')); 
     $('#entrances input:checkbox').each(function(){ 

      if (this.checked) { 
        //console.log($(this).parent('#coords').prevAll('input').val()); 
        //coordinates.push($(this).parent('#coords').prevAll('input').val()); 
        ent_name = $(this).parent('#coords').prevAll('input').map(function() { 
         console.log(ent_name); 
        }).get().change(function() { 
         console.log("We changed"); 
        }); 
        /* 
        ent_name = $(this).parent('#coords').prevAll('input').map(function() { 
         return $(this).val(); 
        }).get(); 


        console.log("Entrance Name: " + ent_name); 
        if (temp_name != ent_name) { 
         coordinates[temp_name] = temp; 
        } else { 
         temp = []; 
        }; 
        var temp_name = ent_name; 
        console.log("Temporary Name: " + temp_name);*/ 

       switch(this.name) { 

        case "dec_coord": 
         temp.push($(this,'input').next().children('input').map(function() { 
          return $(this).val(); 
         }).get()); 
         break; 
        case "deg_coord": 
         var temp2 = $(this,'input').next().children("input,select").map(function() { 
          return $(this).val(); 
         }).get().join(";"); 
         //console.log(temp); 
         temp2 = temp2.toString(); 
         temp2 = temp2.replace(";","°"); 
         temp2 = temp2.replace(";","'"); 
         temp2 = temp2.replace(";","\""); 
         temp2 = temp2.replace(";",","); 
         temp2 = temp2.replace(";","°"); 
         temp2 = temp2.replace(";","'"); 
         temp2 = temp2.replace(";","\""); 
         temp.push(temp2.split(",")); 
         break; 
        case "utm_coord": 
         temp.push($(this,'input').next().children('input,select').map(function() { 
          return $(this).val(); 
         }).get().join(",")); 
         break; 
       } 

      } else { 
       //console.log("wrong"); 
      }; 



     }); 

     $('#dialog').dialog({ 
      width: 500, 
      height: 400, 
      open: function() { 
       //loadMap(); 
      } 
     }); 
    }); 
    /* 
      This is the replication of the Entrance Fields 
      We will limit the number of entrances to 5 
     */ 

     var template = $('#entrances .ent_clone:first').clone(); 
      var cloneCount = 0; 

      var addEntrance = function(){ 
       cloneCount++; 
       var entrance = template.clone().find(':input').each(function() { 
        var newID = this.id+cloneCount; 
        //$(this).prev().attr('for', newID); 
        this.id = newID; 
       }).end() 
       .attr('id', 'ent' + cloneCount) 
       .appendTo("#ent"); 
      }; 
      $('#addEnt').click(addEntrance); 
}); 
+2

문제를 해결 한 경우 자신의 질문에 대한 답변을 게시 한 다음 대답을 수락 할 수 있습니다. 이것은 공식적으로 질문을 "닫고"다른 사람들을 도울 것이므로이 문제에 대한 해결책이 있음을 알게 될 것입니다. – JasCav

답변

0

배열에 각 ent_name를 추가했다을하고이를 비교하는 반복 값이 . . 그래서 나는이 한 : 그 후

ent_name.push($(this).parent('#coords').prevAll('input').val()); 
    var testent = $(this).parent('#coords').prevAll('input').val(); 

을 나는 루프에서 가장 최근 값으로 배열의 첫번째 값을 비교, if 문을 만들었습니다.

if (ent_name[i].toString() != testent) 

그 후 나는 ent_name 객체 속성을 만들고 그 속성 값에 배열을 추가했습니다.

coordinates[ent_name[i]] = temp; 

그런 다음 최근 좌표를 저장하고 있던 임시 배열을 삭제했습니다.

temp = []; 

마지막으로, 나는이 일을하여 최신 ent_name 내 비교 변수를 증가 :

i = ent_name.length; 

당신은 좌표 [ent_name [내가] = 온도를 추가해야합니다; 마지막 루프 이후에 마지막 요소를 객체에도 저장할 수 있습니다.

그래서 종료 코드는 다음을이었다

 ent_name.push($(this).parent('#coords').prevAll('input').val()); 
    var testent = $(this).parent('#coords').prevAll('input').val(); 
        //console.log(ent_name[i++]); 
        //console.log(ent_name); 

        //console.log(temp); 
        if (ent_name[i].toString() != testent) { 
         //console.log(temp); 
         console.log(ent_name); 
         coordinates[ent_name[i]] = temp; 
         temp = []; 
         i = ent_name.length; 
        } 

간단하고 너무 많은 시간을 낭비 저 바보하지만 난 많은 것을 배웠습니다.

건배! Ratty

0

내가 여기 모든 것을 다음 아니에요 ... 말에 따라 변경되지 않습니다, 그러나 그것은 소리 각 입구에 대한 위도/경도 데이터 배열이 있습니다.

ent_name 당신은 아마도 자바 스크립트 객체 변경을 기대하지 않는 고유 키 참 인 경우 : 내가해야 할 일을했을 무엇

var myEntrances = {}; 
... 
myEntrances[ent_name] = temp; 
+0

글쎄, 그게 문제 야. 제가하려는 것은 이것입니다.복제 된 양식의 입력에서 입구의 이름을 가져 와서 해당 속성 이름에 좌표 배열을 저장합니다. 배열의 모든 값을 입구 속성에 저장한다는 것이 문제입니다. – Ratty

+0

그래서 첫 번째 생각은 이전 이름과 이전 이름을 비교하여 루프가 다를지라도 값이 루프 내에서 변경 될 수 있도록하는 것이 었습니다. 나는 여기에 간단한 논리를 놓치고 있다고 생각한다. – Ratty

+0

내가 알아 차 렸던 한 가지 간단한 일은 ... #include #include #include #include #include에 #include 대신 #include를 추가해야합니다. – afranz409

관련 문제