Pickledegg
问问题
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"));
查询的输出已经是一个选项数组,它们是选择的后代,因此您不需要将它们推入另一个数组。或者,如果您的选择没有 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 回答