8663 次
4 に答える
8
これはうまくいくはずです:
$(document).ready(function(){
// array of option elements' values
var optionValues = [];
// array of option elements' text
var optionTexts = [];
// iterate through all option elements
$('#sel > option').each(function() {
// get value/text and push it into respective array
optionValues.push($(this).val());
optionTexts.push($(this).text());
});
// test with alert
alert(optionValues);
alert(optionTexts);
});
select
要素に ID selがあるとします。
于 2008-10-23T21:55:37.900 に答える
6
jQuery.map関数が探しているものかもしれません。以下のコードは、オプションのすべての値またはテキスト値を含む配列を作成し<select>
ます。
var values = jQuery.map(jQuery("#select")[0].options, function(option)
{
return option.value;
});
var texts = jQuery.map(jQuery("#select")[0].options, function(option)
{
return option.innerHTML;
});
于 2008-10-23T22:22:33.633 に答える
2
あなたがする必要があるのは、括弧なしで最初のパラメーターとして配列を渡すことだけです。ブラケットは新しい配列を作成しますが、既に配列を渡しているため、その必要はありません。ただ行う:
$("#CityLocal").autocompleteArray(
MyBigArrayOfOptions,
{
delay:10,
minChars:1,
matchSubset:1,
onItemSelect:selectItem,
onFindValue:findValue,
autoFill:true,
maxItemsToShow:10
}
);
于 2008-10-24T01:27:25.397 に答える
2
あなたの質問を正しく理解できれば、次のコードで必要なことを実行できます。
myFunction($("#my-select option"));
クエリの出力は、選択の子孫であるオプションの配列であるため、それらを別の配列にプッシュする必要はありません。または、select に id がないが、DOM 要素がある場合:
myFunction($("option", theSelect));
このアイデアをコードに組み込むと、次のようになります。
$("#CityLocal").autocompleteArray(
$("option", theSelect),
{
delay:10,
minChars:1,
matchSubset:1,
onItemSelect:selectItem,
onFindValue:findValue,
autoFill:true,
maxItemsToShow:10
}
);
于 2008-10-24T02:06:46.223 に答える