2013-11-27 2 views
0

나는 값을 얻기 위해 반복 할 양식 요소 목록을 가지고 있으므로 입력란에 이름을 입력 한 경우 선택 상자에서 옵션을 선택한 경우 해당 이름을 원할 경우 숫자가 필요하지 않습니다. value하지만 문자열. 이 모든 값은 하나의 문자열로 출력되어야합니다. 루프에서 양식 요소 값을 추출하는 방법은 무엇입니까?

모든 형태의 요소가 누군가가 날 지점 수 있다면 credit_

로 시작하는 이름을 가진 .. 내가 그러나 방법이 문제에 대해 가야할지 몰라, 내가 만든 루프 옳은 방향으로 많이 평가 될 것입니다.

$(this).parent().parent().find('[name*=credit_]').each(function(index){ 

}); 

내 HTML은 아주 간단합니다.

<div class="comp-row"> 
    <!-- a select --> 
    <!-- an input --> 
</div> 

이 양식의 일부는, 많은 다른 양식 필드하지만 필자는 많은 조작 "COMP-행"내 사람과 메신저에만 관심이있다.

$('.comp-row [name*="credit_"]:not([type=hidden])') 
.each(function(index,elem) 
{ 
    console.log($(this).text() != '' ? $(this).find('option:selected').text().trim() : $(this).val()); 
}); 
} 
+2

무엇 당신의 HTML이 생겼 :

.text()

if($('input[name*="credit_"]').text() != '') 평가와 같은 결합 뭔가를이 결합 문제, 당신의 텍스트 값을 읽으려면? – qwertynl

+0

나는 @qwertynl에 동의한다. 이것을 보는 것이 유용 할 것이다. 어쩌면 jsFiddle 또는 유사점을 출발점으로 설정할 수 있습니까? –

+0

'select'와'input' 권한의 이름 만 설정합니까? name 속성이'option' 태그에 대해 유효하지 않습니다. – mschr

답변

1

개봉 $('select[name*="credit_"]>option:selected') 선택을 찾고.

var theName = $('input[name*="credit_"]').text() != '' 
    ? $('select[name*="credit_"]>option:selected').text() 
    : $('input[name*="credit_"]').text(); 
1

형식에 따라 당신이 serialize() 또는 serializeArray() 사용할 수 있습니다 원하는 :

내가 사용하여 끝났다.

$('[name*=credit_]').serializeArray() 

serialize() API docs

serializeArray() API docs

+0

아 아뇨 이것은 양식의 일부입니다. 양식 내에 다른 많은 양식 필드가 있지만 섹션에만 관련된 것입니다. – Iamsamstimpson

+1

은 그림과 같이 이러한 요소를 격리합니다. 샘플 HTML을 제공합니다. – charlietfl

+1

'.serialize'는 선택 옵션의 값을 얻을 것이고, 그는 텍스트를 원합니다. – Barmar

1
var result = ''; 
$(this).parent().parent().find('[name*=credit_]').each(function(index){ 
    result += $(this).is("select") ? $(this).text() : $(this).val(); 
}); 
0
012,341 : 요소의 특정 그룹의

var data=$('#myForm').serialize() 

예는 전체 양식 수득하는

기준 (name*=credit_)과 일치하는 모든 요소를 ​​반복합니다. 유형을 확인하고 변수에 값을 입력하십시오.

HTML

$(function() { 
    var values = ''; 

    $('form [name*=credit_]').each(function() { 
     var $this = $(this); 

     if($this[0].tagName == 'TEXTAREA') { 
      values += ' ' + $this.text(); 
     } 
     else if ($this[0].tagName == 'SELECT') { 
      values += ' ' + $this.find(':selected').text(); 
     } 
     else { 
      values += ' ' + $this.val(); 
     } 
    }); 

    $('#result').html(values);  
}); 

jsfiddle

<form> 
    <input type="text" name="credit_a" value="1" /> 
    <input type="text" name="credit_b" value="2" /> 
    <input type="text" name="credit_c" value="3" /> 
    <select name="credit_d"> 
     <option value="kk">kk</option> 
     <option value="jj" selected>jjjjj</option> 
    </select> 
    <input name="credit_e type="checkbox" checked value="imchecked" /> 
</form> 

<form> 
    <input type="text" name="credit_a" value="55" /> 
    <input type="text" name="credit_b" value="66" /> 
    <input type="text" name="credit_c" value="77" /> 
    <input type="text" name="credit_d" value="88" /> 
</form> 

<p id="result"> </p> 

자바 스크립트 :

관련 문제