I'm trying to get the text of some selected options from a select list with the jQuery.each function.
我正在尝试用jQuery从一个选择列表中获取一些选择选项的文本。每个函数。
I select all selected options like this: $('#IdOfSelect option:selected')
This works just fine.
我选择了所有选择的选项:$('#IdOfSelect选项:selected'),这很好。
If I iterate over this jQuery-object I always get the error value.text()
is not a function when I try to get the text of my single option.
如果我遍历这个jquery对象,我总是会得到error value.text()不是一个函数,当我试图获取单个选项的文本时。
You can see this behaviour here: http://jsfiddle.net/z2nbP/
您可以在这里看到这种行为:http://jsfiddle.net/z2nbP/
In the Firebug-Console the item shows as <option value="10">
instead of my expected [option]
(if it's a DOM-object).
在firebug控制台中,项目显示为< options value="10">,而不是我期望的[option](如果是DOM-object)。
How is it possible to iterate over those selected options and although get there texts?
如何能够遍历这些选定的选项并获得文本?
4 个解决方案
#1
11
You need this:
你需要:
$(value).text()
because the value
parameter passed to the callback is the native DOM element which doesn't have the .text()
method defined on it.
因为传递给回调的值参数是本地DOM元素,它没有定义的.text()方法。
Here's the updated demo.
这是更新后的演示。
#3
0
value is not a jquery object, you have to make it as a jquery object in this way-
值不是jquery对象,您必须以这种方式将其作为jquery对象
$(value).text();
#4
0
Try this:
试试这个:
$('#result').append($(this).val()+'
');
((“#结果”).append美元(这).val()+ ');
#1
11
You need this:
你需要:
$(value).text()
because the value
parameter passed to the callback is the native DOM element which doesn't have the .text()
method defined on it.
因为传递给回调的值参数是本地DOM元素,它没有定义的.text()方法。
Here's the updated demo.
这是更新后的演示。
#2
#3
0
value is not a jquery object, you have to make it as a jquery object in this way-
值不是jquery对象,您必须以这种方式将其作为jquery对象
$(value).text();
#4
0
Try this:
试试这个:
$('#result').append($(this).val()+'
');
((“#结果”).append美元(这).val()+ ');