2011-09-22 5 views
0

가능한 중복 :배열 이름 jQuery로 HTML 요소를 읽어

<ul id="mytags" class="tagit"> 
    <li class="tagit-choice"> 
     Acrylic 
     <input type="hidden" name="item[tags][]" value="Acrylic" style="display: none;"> 
     </li> 
    <li class="tagit-choice"> 
     Bath 
    <input type="hidden" name="item[tags][]" value="Bath" style="display: none;"> 
    </li> 
</ul> 
: 수 나는 다음과 같은 HTML 문서에서 item[tags][]라는 필드의 값을 읽는 방법

How to get a value of an element by name instead of ID

+2

'hidden' 필드가 렌더링되지 않습니다. –

+0

또한 http://api.jquery.com/attribute-equals-selector/에서 살펴보십시오 ... 모든 것이 있습니다. –

+0

또한 * 이전에 요청한 모든 질문에 대한 답변을 수락하십시오. 숙련 된 사용자는 질문에 더 기꺼이 대답 할 것이며 나중에 참조 할 때 유용합니다. – thomaux

답변

0

이 페이지에서 고유 할 필요가 없습니다 값 받기를 원한다면 그리고 당신은 디스플레이 아무도 사용하지 야해. 동일한 name 속성을 갖는 값은 양식 제출시 자동으로 배열로 전달됩니다.

JS 배열에 요소 값을 채우는 예제를 만들었습니다.

마크 업 :

<ul id="mytags" class="tagit"> 
    <li class="tagit-choice"> 
    Acrylic 
    <input type="hidden" name="tags" value="Acrylic"> 
    </li> 
    <li class="tagit-choice"> 
    Bath 
    <input type="hidden" name="tags" value="Bath"> 
    </li> 
</ul> 

코드

var tags = new Array[]; 
$("#mytags").find("[name='tags']").each(
    function() { 
     tags.push($(this).val()); 
    } 
); 
alert(tags.toString()); 

당신은 그것으로 재생할 수 있습니다 : none` : 당신은`디스플레이를 필요가 없습니다 http://jsfiddle.net/VAjeN/

0
$(document).ready(function() { 
    $('input[name="item[tags][]"]').each(function(){ 
     alert($(this).val()); 
    }); 
}); 
1

이것을 사용하여 이름이 'something'인 입력 값을 가져올 수 있습니다. 당신은 당신이 name부터 배열로 값을 전달하기 위해 괄호를 사용할 필요가 없습니다

$('input[name=something]').val() 
관련 문제