我已经在这里看到了其他的例子,但没有一个适合我的问题。使用jQuery遍历select元素中的选项
我有一个函数接收一个select
元素,我需要它来吐出一个JSON格式的字符串。
例子:
foo = buildOptionStructure($('.select-test'));
// Returns string of JSON formatted option data from select
// options in element.
function buildOptionStructure(selectElement) {
options = "[";
selectElement.find('option').each(function(option, i) {
console.log(this); // Return my html in console.
console.log(option); // Return a zero based index!
console.log("I VALUE: " + option); // Returns same zero based index!
options += "{";
options += "name:'" + option.text() + "'";
options += "},";
});
options += "]"
return options;
}
我只需要建立一个串出在给定的选择元素的选项。我在这里错过了什么?
你错过了在该代码的报价。你也没有从你的函数返回任何东西。 –
@james:我编辑了缺少引号的问题。 – sergserg