2011-05-12 7 views
0

와 select 태그의 모든 요소를 ​​반환 나는이처럼 보이는 select 태그가 :JQuery와

<select name="TZSelector" id="TheTZSelector"> 
    <option>add another timezone</option> 
    <option>------------------------------------------------------</option> 
    <option></option> 
    <option value="-8.00">(Pacific Time) San Francisco</option> 
    <option value="-7.00">(Mountain Time) Denver 
...... 
</select> 

가 어떻게 옵션에있는 모든 값을 반환 할 수 있습니다. 옵션 1,2와 3에는 값이 없지만 다른 모든 값은 값을가집니다. 나는 이런 식으로 뭔가를 찾고 있어요 :

$(...).each(function() { MyFunction takes the value of $(this) as parameter }); 

그런 식으로, 내가하여 myFunction 작성하고 옵션의 컬렉션을 반복 할 수 있습니다.

의견을 보내 주셔서 감사합니다.

답변

1

이렇게하면 값이있는 모든 옵션이 선택됩니다.

$('select > option[value]').each(function() { 
    // Your magic here 
    // $(this).val() is the value of the option 
}); 

바이올린 :http://jsfiddle.net/8PHDp/1/

+0

그것 (('# TheTZSelector> 옵션 [값]) 각 (함수() { 경고 $이어야한다 $ (this)); }); 그것은 보여주고 있습니다 [Object] [Object] – frenchie

+0

예, 당신의 HTML이 완벽 할 것입니다! 샌프란시스코와 덴버라는 두 가지 값이 있으므로 두 가지 객체가 표시되어야합니다. –

0
function get_opt_vals($select){ 
    var all_val = []; 
    $('option', $select).each(function(index, item){ 
     all_val.push(item.value); 
    }) 
    return all_val; 
} 

//call like this: 
var opt_array = get_opt_vals($('#TheTZSelector')); 

console.log(opt_array); 

바이올린 :. http://jsfiddle.net/maniator/yqxX4/