2010-02-26 4 views
0

테이블에 두 개의 열이 있고 행이 동적으로 만들어집니다.열당 테이블 값 읽기

<table id ="datatable"> 
    <tr> 
    <td> 
     <input type ="text"> 
    </td> 
    <td> 
     <input type ="text"> 
    </td> 
    </tr> 
</table> 

각 행에 대해 각 테이블의 값을 가져 와서 다음과 같이 개체에 삽입하고 싶습니다.

var reading= new Object(); 
reading.Name = (column1).value 
reading.Value = (column2).value 

어떻게하면됩니까?

답변

1
//fetching lines 
$('table tr').each(function(){ 
    var reading = new Object(); 

    reading.name = $(this).find('td:first input').val(); 
    reading.value= $(this).find('td:last input').val(); 

}); 

업데이트 : 설정 오류 메시지가

$('table tr').each(function(){ 
    var reading = new Object(); 

    name = $(this).find('td:first'); 
    reading.name = name.find('>input').val(); 
    if(reading.name == '') 
    { 
    name.append('<span>field empty</span>'); 
    } 

    value = $(this).find('td:last'); 
    reading.value = value.find('>input').val(); 
    if(reading.value == '') 
    { 
    value.append('<span>field empty</span>'); 
    } 

}); 
+0

위의 코드를 사용하면 헤더 값을 읽는 것도 알 수 있습니다. 헤더 값을 읽지 않으려면 어떻게해야합니까? – learning

+0

헤더 값은 무엇을 의미합니까? 태그 또는 무엇을 의미합니까?) – Skay

+0

예 태그. 다른 질문을하고 싶습니다. 입력 값이 null 인 경우 오류 메시지를 표시하려고합니다. 각 행에 td를 추가하면 어떻게 할 수 있습니까? 나는 노력했고, 증가하는 기둥의 수를 볼 수 있습니다. 또는 훨씬 더 나은 방법, 마지막 레이블을 수정하여 레이블을 삽입하려면 어떻게해야합니까? – learning

0

당신은 테이블 셀에있는 한 다음의 각이 each function를 사용하여 반복 모든 입력 유형을 가져옵니다 선택기를 사용할 수 비어있는 경우 :

// Get the selector. 
var selector = $("#datatable tr td input[type='text']"); 

// Create the object. 
var reading = {}; 

// Cycle through the inputs, attaching the properties. 
selector.each(function (index, element) { 
    // If index is 0, then set the name property. 
    if (index === 0) reading.Name = $(this).val(); 

    // If index is 1, set the value property. 
    if (index === 1) reading.Value = $(this).val(); 
}); 

참고로 선택기가 반드시 정확할 필요는 없습니다. 이 경우에는 #datatabe input과 같은 간단한 선택기가 작동합니다 (모든 입력 유형이 텍스트 필드라고 가정).

코드의 경우 레이아웃의 변경이 쉽기 때문에 입력 코드의 위치 값에 크게 의존하며 이는 좋지 않습니다. 오히려 name 또는 id 속성 또는 사용자 정의 데이터 속성 ("data-"접두사가 붙은 HTML 5이 추가되었지만 4.01 strict의 경우 유효 함)을 사용하여 입력 유형의 속성을 지정할 수 있습니다.

<table id ="datatable">    
    <tr>    
    <td>    
     <input name="Name" type ="text">    
    </td>    
    <td>    
     <input name="Value" type ="text">    
    </td>    
    </tr>    
</table> 

그런 다음과 같이, 속성 값에서 속성 이름을 얻고, 더 유연하게 코드를 변경할 수 있습니다 변경해야

// Get the selector. 
var selector = $("#datatable tr td input[type='text']"); 

// Create the object. 
var reading = {}; 

// Cycle through the inputs, attaching the properties. 
selector.each(function (index, element) { 
    // Get the selector for the element. 
    var thisSelector = $(this); 

    // Set the value accoding to the name on the element. 
    reading[thisSelector.attr("name")] = thisSelector.val(); 
}); 

이 방법, 당신은/추가 속성을 제거 할 수 있습니다 및 아닌 코드에서 요소의 특성을 읽습니다.

+0

<% = Html.textbox() %>를 사용하는 경우 어떻게 텍스트 상자의 이름을 설정할 수 있습니까? – learning

+1

@ user281180 : 요소에 할당 할 특성을 정의하는 개체를 사용하는 ASP.NET MVC에서 TextBox 확장 메서드가 오버로드됩니다. new {name = "Value"}와 같은 익명 유형을 전달할 수 있으며 name 속성에 "Value"를 할당합니다.또한, 당신은 일반적으로 양식 요소에서 사용하기 때문에 텍스트 상자의 이름에도 문자열을 사용하는 오버로드가 있다고 생각합니다. – casperOne

+0

위 코드를 사용하여 헤더 값을 읽는 중임을 알 수 있습니다. 헤더 값을 읽지 않으려면 어떻게해야합니까? – learning