用JavaScript做這樣的
var opt = document.getElementById('idOfYourSelect').options;
for (var i = 0, ii = opt.length; i < ii; i++) {
console.log(opt[i].value);
}
可以使用Array.prototype.map
代替顯式循環。
var options = document.getElementById('test').options;
var values = Array.prototype.map.call(options, function(val) {
return val.value;
});
不過你得把它添加到Array.prototype
如果你支持舊版瀏覽器。
有在MDC的實現可以使用:
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp */)
{
"use strict";
if (this === void 0 || this === null)
throw new TypeError();
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in t)
res[i] = fun.call(thisp, t[i], i, t);
}
return res;
};
}
這不給你值... – eyelidlessness
@eyelidlessness我更多的是「授人以漁」類型的球員;) – Phil
這很好,我想,除了我不認爲它必然是網站的精神。無論如何,最好解釋一下,當你發佈一個實際上並不能解決問題的答案時,你就是這麼做的。 – eyelidlessness